JavaScript(JS) 中实现继承的几种方式

本文详细介绍了JavaScript中的六种继承方式:原型链继承、构造继承、实例继承、拷贝继承、组合继承和寄生组合继承,分析了各自的特点、优缺点以及适用场景。

目录

1、原型链继承

2、构造继承

3、实例继承

4、拷贝继承

5、组合继承

6、寄生组合继承


1、原型链继承

将父类的实例作为子类的原型,他的特点是实例是子类的实例也是父类的实例,父类新增的原型方法/属性,子类都能够访问,并且原型链继承简单易于实现,缺点是来自原型对象的所有属性被所有实例共享,无法实现多继承,无法向父类构造函数传参。

2、构造继承

使用父类的构造函数来增强子类实例,即复制父类的实例属性给子类, 构造继承可以向父类传递参数,可以实现多继承,通过 call 多个父类对象。但是构造继承只能继承父类的实例属性和方法,不能继承原型属性和方法,无法实现函数服用,每个子类都有父类实例函数的副本,影响性能。 

3、实例继承

为父类实例添加新特性,作为子类实例返回,实例继承的特点是不限制调用方法,不管是 new 子类()还是子类()返回的对象具有相同的效果,缺点是实例是父类的实例,不是子类的实例,不支持多继承 。

4、拷贝继承

特点:支持多继承,缺点:效率较低,内存占用高(因为要拷贝父类的属性)无法获取父类不可枚举的方法(不可枚举方法,不能使用 for in 访问到)。

5、组合继承

通过调用父类构造,继承父类的属性并保留传参的优点,然后通过将父类实例作为子类原型,实现函数复用。

6、寄生组合继承

通过寄生方式,砍掉父类的实例属性,这样在调用两次父类的构造的时候,就不会初始化两次实例方法/属性,避免的组合继承的缺点。

<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
发出的红包

打赏作者

Yongqiang Chen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值