js中的几种继承

本文深入探讨JavaScript中的四种主要继承模式:原型链继承、构造继承、实例继承和组合继承,解析每种模式的特点、优缺点及应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

注:

//定义一个父类
function Animal(){
    /*动物类有什么特征和行为*/
    this.name=null;
    this.age=null;
    this.sex=null;
    this.sleep=function(){
        return this.name+"在睡觉";
    }
}
//原型链追加(继承) 给原型方法里面追加属性和行为的
Animal.prototype.eat=function(){
    return this.name+"狗粮";
};
//定义一个子类
function Dog(){
    this.play=function(){
        return this.name+"正在玩球球";
    }
}

一、原型链继承(单继承)

1.核心: 将父类的实例作为子类的原型继承。

2.特点:非常纯粹的继承关系,实例是子类也是父类的实例。父类新增原型方法/原型属性,子类都能访问到。

3.缺点:要想为子类新增属性和方法,必须要在new Animal()这样的语句之后执行,不能放到构造器中无法实现多继承

创建子类实例时,无法向父类构造函数传参。

var a=new Animal(); //实例对象(造出一个新东西)
Dog.prototype=a;  //D大写  dog对象中有了animal的所有属性
Dog.prototype.color=function(){
    return this.name+"是白色";
}
//实例化Dog对象
var c=new Dog();
c.name="哦多都";
c.age="3";
c.sex="母";
console.log(c);
console.log(c.sleep());
console.log(c.play());
console.log(c.color());
console.log(c instanceof Animal); //判断C对象到A对象构造函数的类型是否一致
console.log(c instanceof Dog);

二、构造继承(多继承)

1.核心:父类的实例复制给子类(没用到原型)。

2.特点:创建子类实例时,可以向父类传递参数可以实现多继承(call多个父类对象)。

3.缺点:实例只是子类的实例只能继承父类的实例属性和方法,不能继承原型属性/方法,无法实现函数复用。每个子类都有父类实例函数的副本,影响性能。

function Animal(name,color,sex,age,type){
    /*动物类有什么特征和行为*/
    this.name=name;
    this.AnimalColor=color;
    this.sex=sex;
    this.age=age;
    this.type=type;
    this.sleep=function (){
        return this.name+"会睡觉";
    }
}
Animal.prototype.solo=function (){
    return "哈哈"
}
//另一个父类
function rou(name){
    this.name=name;
    this.eat=function (){
        return this.name+"吃肉";
    }
}
//定义一个子类对象
function Cat(name,color,sex,age,type){
    Animal.call(this,name,color,sex,age,type);
    rou.apply(this,[name]);
}
var c=new Cat("小猫","白色","母",2,"猫");
console.log(c);
console.log(c.eat());
console.log(c instanceof Cat);
console.log(c instanceof  Animal);
console.log(c instanceof  rou);

注:call和apply在替换上无区别,在参数上有区别。

Animal.call(this,name,color,sex,age,type);
rou.apply(this,[name]);
//定义一个父类
function Animal(){
    this.name=null;    //此this指animal    替换后指dog
}
//定义一个子类
function Dog(){
    Animal.call(this);  //用dog去替换animal中的所有对象 此this指dog 则替换后之前的this全部指dog而不是animal
    Animal.apply(this);
    //call和apply在替换上无区别,在参数上有区别
}

三、实例继承(单继承)

1.特点:不限制调用方式,不管是实例化对象,返回的对象具有相同的效果

2.缺点:实例是父类的实例,不是子类的实例,不支持多继承

/*先去定义一个父类对象*/
function Animal(){
    /*动物类有什么特征和行为*/
    this.name=null;
}
//定义一个子类对象
function Cat(){
    var a=new Animal();
    return a;
}
console.log(Cat());
//直接调用方法0或者实例化对象
var c=new Cat();
console.log(c);
/*先去定义一个父类对象*/
function Animal(){
    /*动物类有什么特征和行为*/
    this.name=null;
    this.AnimalColor=null;
    this.sex=null;
    this.age=null;
    this.type=null;
    this.sleep=function (){
        return this.name+"会睡觉";
    }
}
Animal.prototype.speed=function (){
    return 5.0;
}
//定义一个子类对象
function Cat(){
    var a=new Animal();
    a.name="小猫";
    return a;
}
var c=new Cat();
console.log(c);
console.log(c.speed());
console.log(c instanceof  Cat);
console.log(c instanceof Animal);

四、组合继承

原型链继承要放到子类实例之前。

/*先去定义一个父类对象*/
function Animal(){
    /*动物类有什么特征和行为*/
    this.name=null;
    this.AnimalColor=null;
    this.sex=null;
    this.age=null;
    this.type=null;
    this.sleep=function (){
        return this.name+"会睡觉";
    }
}
Animal.prototype.speed=function (){
    return 5.0;
}
//构造继承
function rou(){
    this.eat=function (){
        return "吃肉";
    }
}
//定义一个子类对象
function Cat(){
    Animal.call(this);
    rou.call(this);
}
//构造继承无法继承原型属性/方法,所以写一个原型继承,可拿到speed
Cat.prototype=new Animal();
console.log(c.speed());

var c=new Cat();
console.log(c);
console.log(c.eat());
<think>我们被要求回答JS继承实现的几种方式,并在最后提出3-5个相关问题。 首先,我们需要列举并解释JavaScript中实现继承几种主要方式,包括原型链继承、构造函数继承、组合继承、原型式继承、寄生式继承、寄生组合式继承,以及ES6的class继承。 然后,按照规则,在回答的最后,以“§§相关问题§§”开头,提出3-5个相关问题。 注意:回答必须使用中文,并且相关问题必须基于用户的问题和我们的回答。 下面开始回答:</think>### JavaScript 继承实现方式详解 #### 1. **原型链继承** ```javascript function Parent() { this.name = 'Parent'; } Parent.prototype.say = function() { return this.name; }; function Child() {} Child.prototype = new Parent(); // 核心:继承父类实例 const child = new Child(); console.log(child.say()); // "Parent" ``` **特点**: - 子类共享父类原型方法 - 缺点:所有子类共享父类实例属性(引用类型会互相影响) #### 2. **构造函数继承** ```javascript function Parent(name) { this.name = name; this.colors = ['red', 'blue']; } function Child(name) { Parent.call(this, name); // 核心:复制父类实例属性 } const c1 = new Child('Tom'); c1.colors.push('green'); const c2 = new Child('Jerry'); console.log(c2.colors); // ["red", "blue"] ``` **特点**: - 解决引用属性共享问题 - 缺点:无法继承父类原型方法 #### 3. **组合继承(经典继承)** ```javascript function Parent(name) { this.name = name; } Parent.prototype.say = function() { return this.name; }; function Child(name) { Parent.call(this, name); // 第1次调用Parent } Child.prototype = new Parent(); // 第2次调用Parent Child.prototype.constructor = Child; const child = new Child('Alice'); console.log(child.say()); // "Alice" ``` **特点**: - 结合原型链和构造函数的优点 - 缺点:父类构造函数被调用两次 #### 4. **原型式继承** ```javascript const parent = { name: 'Parent', say() { return this.name; } }; const child = Object.create(parent); // 核心方法 child.name = 'Child'; console.log(child.say()); // "Child" ``` **特点**: - 类似原型链继承的轻量实现 - 适用于简单对象继承 #### 5. **寄生式继承** ```javascript function createChild(original) { const clone = Object.create(original); // 创建新对象 clone.sayHi = function() { // 增强对象 return `Hi, ${this.name}`; }; return clone; } const child = createChild({ name: 'Bob' }); console.log(child.sayHi()); // "Hi, Bob" ``` **特点**: - 工厂函数模式增强对象 - 缺点:方法无法复用 #### 6. **寄生组合式继承(最优解)** ```javascript function inherit(Child, Parent) { const prototype = Object.create(Parent.prototype); // 创建父类原型副本 prototype.constructor = Child; // 修复constructor Child.prototype = prototype; // 指定子类原型 } function Parent(name) { this.name = name; } Parent.prototype.say = function() { return this.name; }; function Child(name) { Parent.call(this, name); } inherit(Child, Parent); // 实现继承 const child = new Child('Eve'); console.log(child.say()); // "Eve" ``` **特点**: - 只调用次父类构造函数 - 保持原型链完整 - ES5 最完美的继承方案 #### 7. **ES6 Class 继承** ```javascript class Parent { constructor(name) { this.name = name; } say() { return this.name; } } class Child extends Parent { // 核心语法 constructor(name) { super(name); // 必须调用super } } const child = new Child('Grace'); console.log(child.say()); // "Grace" ``` **特点**: - 语法糖(底层基于寄生组合式继承) - 内置super关键字访问父类 - 支持静态方法继承 #### 继承方式对比表 | 继承方式 | 原型方法继承 | 实例属性隔离 | 调用父类次数 | 原型链完整性 | |------------------|--------------|--------------|--------------|--------------| | 原型链继承 | ✅ | ❌ | 1次 | ✅ | | 构造函数继承 | ❌ | ✅ | 多次 | ❌ | | 组合继承 | ✅ | ✅ | 2次 | ✅ | | 寄生组合式继承 | ✅ | ✅ | 1次 | ✅ | | ES6 Class继承 | ✅ | ✅ | 1次 | ✅ | #### 实际应用场景 - **UI组件库开发**:使用ES6 Class实现组件层级继承 - **工具类扩展**:寄生式继承扩展第三方库功能 - **数据模型定义**:组合继承实现模型属性和方法的分离 - **框架核心类**:寄生组合式保持最佳性能
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值