原型链继承
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| 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())
|
缺点:
引用(对象)类型的属性被所有实例共享
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| function Parent () { this.names = ['kevin', 'daisy']; }
function Child () {
}
Child.prototype = new Parent();
var child1 = new Child(); child1.names.push('yayu'); console.log(child1.names);
var child2 = new Child(); console.log(child2.names);
|
在创建 Child 的实例时,不能向Parent传参
构造函数继承
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| function Parent (name) { this.names = [name, 'daisy']; }
function Child (name) { Parent.call(this, name); }
var child1 = new Child('kevin1'); child1.names.push('yayu'); console.log(child1.names);
var child2 = new Child('kevin2'); console.log(child2.names);
|
优点:
避免了引用(对象)类型的属性被所有实例共享
可以在 Child 中向 Parent 传参
缺点:只能继承父类的实例属性和方法,不能继承原型属性或者方法。
组合继承
融合原型链继承和构造函数的优点,是 JavaScript 中最常用的继承模式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| 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(); Child.prototype.constructor = Child;
var child1 = new Child('kevin', '18'); child1.colors.push('black');
console.log(child1.name); console.log(child1.age); console.log(child1.colors);
var child2 = new Child('daisy', '20');
console.log(child2.name); console.log(child2.age); console.log(child2.colors);
|
缺点:会调用两次父构造函数。所以,在上述例子中,如果我们打印 child1 对象,我们会发现 Child.prototype 和 child1 都有一个属性为colors,属性值为['red', 'blue', 'green']。
原型式继承
就是 ES5 Object.create 的模拟实现,将传入的对象作为创建的对象的原型。
1 2 3 4 5
| function createObj(o) { function F(){} F.prototype = o; return new F(); }
|
缺点:因为Object.create方法实现的是浅拷贝,多个实例的引用类型属性指向相同的内存,存在篡改的可能。
1 2 3 4 5 6 7 8 9 10 11 12 13
| var person = { name: 'kevin', friends: ['daisy', 'kelly'] }
var person1 = createObj(person); var person2 = createObj(person);
person1.name = 'person1'; console.log(person2.name);
person1.friends.push('taylor'); console.log(person2.friends);
|
注意:修改person1.name的值,person2.name的值并未发生改变,并不是因为person1和person2有独立的 name 值,而是因为person1.name = 'person1',给person1添加了 name 值,并非修改了原型上的 name 值。
寄生式继承
创建一个仅用于封装继承过程的函数,该函数在内部以某种形式来做增强对象,最后返回对象。
1 2 3 4 5 6 7
| function createObj (o) { var clone = Object.create(o); clone.sayName = function () { console.log('hi'); } return clone; }
|
缺点:跟上面讲的原型式继承一样。
寄生组合式继承
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| 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();
Child.prototype.constructor = Child
var child1 = new Child('kevin', '18'); console.log(child1);
|
优点:只调用了一次 Parent 构造函数,并且因此避免了在 Parent.prototype 上面创建不必要的、多余的属性。与此同时,原型链还能保持不变;因此,还能够正常使用 instanceof 和 isPrototypeOf。
class继承
利用babel工具进行转换,我们会发现extends实际采用的也是寄生组合继承方式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| class Animal { constructor(name) { this.name = name; }
hello() { console.log("hello"); } }
class Dog extends Animal { constructor(name, say) { super(name); this.say = say; } }
|
参考