1、原型链继承
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())
2、借用构造函数(经典继承)
function Parent () {
this.names = ['kevin', 'daisy'];
}
function Child () {
Parent.call(this);
}
var child1 = new Child();
child1.names.push('yayu');
console.log(child1.names);
var child2 = new Child();
console.log(child2.names);
- 优点:
- 避免了引用类型的属性被所有实例共享
- 可以在 Child 中向 Parent 传参
- 举个例子:
function Parent(name) {
this.name = name
}
function Child(name) {
Parent.call(this, name)
}
var child1 = new Child('kevin');
console.log(child1.name);
var child2 = new Child('daisy');
console.log(child2.name);
- 缺点:
- 方法都在构造函数中定义,每次创建实例都会创建一遍方法
3、组合继承
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);
- 优点:
- 融合原型链继承和构造函数的优点,是 JS 中最常用的继承模式
4、原型式继承
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);
person1.firends.push('taylor');
console.log(person2.friends);
- 注意:修改 person1.name 的值,person2.name 的值并未发生改变,并不是因为 person1 和 person2 有独立的 name 值,而是因为 person1.name = ‘person1’,给 person1 添加了 name 值,并非修改了原型上的 name 值。
5、寄生式继承
- 创建一个仅用于封装继承过程的函数,该函数在内部以某种形式来做增强对象,最后返回对象
function createObj(o) {
var clone = Object.create(o)
clone.sayName = function () {
console.log('hi')
}
return clone
}
- 缺点:跟借用构造函数模式一样,每次创建对象都会创建一遍方法。
6、寄生组合式继承
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)
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。开发人员普遍认为寄生组合式继承是引用类型最理想的继承范式。