本文系统梳理JavaScript中六种继承模式的实现原理与优劣,涵盖原型链、借用构造函数、组合继承、原型式、寄生式及寄生组合式继承。重点解析各模式在属性共享、传参支持及内存效率上的差异,并论证寄生组合式继承为何被视为引用类型最理想的继承范式。
前言部分
本文将详细解析JavaScript中多种继承模式的实现机制及其各自的优缺点。
注意事项:
与《JavaScript深入之创建对象》一文风格类似,本文主要作为技术笔记整理。
此处不禁再次感叹:《JavaScript高级程序设计》一书编写得极为出色。
原型链继承
一、原型链继承
function Parent () {
this.name = 'kevin';
}
Parent.prototype.getName = function () {
console.log(this.name);
}
function Child () {
}
Child.prototype = new Parent();
var child1 = new Child();
console.log(child1.getName()) // kevin
该模式存在以下主要问题:
1. 引用类型的属性会被所有实例共享,具体示例如下:
function Parent () {
this.names = ['kevin', 'daisy'];
}
function Child () {
}
Child.prototype = new Parent();
var child1 = new Child();
child1.names.push('yayu');
console.log(child1.names); // ["kevin", "daisy", "yayu"]
var child2 = new Child();
console.log(child2.names); // ["kevin", "daisy", "yayu"]
2. 在创建 Child 实例时,无法向 Parent 构造函数传递参数。
借用构造函数(经典继承)
二、借用构造函数(经典继承)
function Parent () {
this.names = ['kevin', 'daisy'];
}
function Child () {
Parent.call(this);
}
var child1 = new Child();
child1.names.push('yayu');
console.log(child1.names); // ["kevin", "daisy", "yayu"]
var child2 = new Child();
console.log(child2.names); // ["kevin", "daisy"]
该模式具备以下优点:
1. 有效避免了引用类型属性被所有实例共享的问题。
2. 支持在 Child 构造函数中向 Parent 传递参数。
具体示例如下:
function Parent (name) {
this.name = name;
}
function Child (name) {
Parent.call(this, name);
}
var child1 = new Child('kevin');
console.log(child1.name); // kevin
var child2 = new Child('daisy');
console.log(child2.name); // daisy
该模式的主要缺点:
由于方法定义在构造函数内部,每次创建实例时都会重新创建一遍方法,造成资源浪费。
组合继承
三、组合继承
该模式结合了原型链继承与经典继承的优势。
function Parent (name) {
this.name = name;
this.colors = ['red', 'blue', 'green'];
}
Parent.prototype.getName = function () {
console.log(this.name)
}
function Child (name, age) {
Parent.call(this, name);
this.age = age;
}
Child.prototype = new Parent();
var child1 = new Child('kevin', '18');
child1.colors.push('black');
console.log(child1.name); // kevin
console.log(child1.age); // 18
console.log(child1.colors); // ["red", "blue", "green", "black"]
var child2 = new Child('daisy', '20');
console.log(child2.name); // daisy
console.log(child2.age); // 20
console.log(child2.colors); // ["red", "blue", "green"]
优点:融合了原型链继承和构造函数的优点,是JavaScript中最常用的继承模式。
原型式继承
四、原型式继承
function createObj(o) {
function F(){}
F.prototype = o;
return new F();
}
这实际上是ES5中 Object.create 方法的模拟实现,其核心是将传入的对象作为新创建对象的原型。
该模式的缺点:
包含引用类型的属性值始终都会共享相应的值,这一点与原型链继承相同。
var person = {
name: 'kevin',
friends: ['daisy', 'kelly']
}
var person1 = createObj(person);
var person2 = createObj(person);
person1.name = 'person1';
console.log(person2.name); // kevin
person1.firends.push('taylor');
console.log(person2.friends); // ["daisy", "kelly", "taylor"]
注意:修改 person1.name 的值,person2.name 的值并未发生改变,这并不是因为 person1 和 person2 拥有独立的 name 值,而是因为 person1.name = 'person1',给 person1 添加了 name 值,并非修改了原型上的 name 值。
寄生式继承
五、寄生式继承
该模式创建一个仅用于封装继承过程的函数,该函数在内部以某种形式来增强对象,最后返回该对象。
function createObj (o) {
var clone = object.create(o);
clone.sayName = function () {
console.log('hi');
}
return clone;
}
缺点:与借用构造函数模式一样,每次创建对象都会重新创建一遍方法。
寄生组合式继承
六、寄生组合式继承
为了方便阅读,在此处重复展示组合继承的代码:
function Parent (name) {
this.name = name;
this.colors = ['red', 'blue', 'green'];
}
Parent.prototype.getName = function () {
console.log(this.name)
}
function Child (name, age) {
Parent.call(this, name);
this.age = age;
}
Child.prototype = new Parent();
var child1 = new Child('kevin', '18');
console.log(child1)
组合继承最大的缺点是会调用两次父构造函数。
第一次调用发生在设置子类型实例的原型时:
Child.prototype = new Parent();
第二次调用发生在创建子类型实例时:
var child1 = new Child('kevin', '18');
回顾 new 操作符的模拟实现,实际上在这句代码执行时,我们会执行:
Parent.call(this, name);
在这里,我们又调用了一次 Parent 构造函数。
因此,在上述例子中,如果打印 child1 对象,会发现 Child.prototype 和 child1 实例上均存在一个名为 colors 的属性,且属性值均为 ['red', 'blue', 'green']。
那么,我们该如何进一步优化,避免这次重复调用呢?
如果我们不使用 Child.prototype = new Parent(),而是间接让 Child.prototype 访问到 Parent.prototype 呢?
具体实现方式如下:
function Parent (name) {
this.name = name;
this.colors = ['red', 'blue', 'green'];
}
Parent.prototype.getName = function () {
console.log(this.name)
}
function Child (name, age) {
Parent.call(this, name);
this.age = age;
}
// 关键的三步
var F = function () {};
F.prototype = Parent.prototype;
Child.prototype = new F();
var child1 = new Child('kevin', '18');
console.log(child1);
最后,我们将此继承方法封装起来:
function object(o) {
function F() {}
F.prototype = o;
return new F();
}
function prototype(child, parent) {
var prototype = object(parent.prototype);
prototype.constructor = child;
child.prototype = prototype;
}
// 当我们使用的时候:
prototype(Child, Parent);
引用《JavaScript高级程序设计》中对寄生组合式继承的评价:
这种方式的高效率体现在它仅调用了一次 Parent 构造函数,从而避免了在 Parent.prototype 上创建不必要的、多余的属性。与此同时,原型链还能保持不变;因此,还能够正常使用 instanceof 和 isPrototypeOf 方法。开发人员普遍认为寄生组合式继承是引用类型最理想的继承范式。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。