1. 原型链继承

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function Parent(){
this.name = '缪克立';
}

Parent.prototype.getName = function(){
return this.name
}

function Child(){

}

Child.prototype = new Parent();
var child1 = new Child();
console.log(child1.getName()) // 缪克立

问题:

  1. 引用类型的属性被所有实例共享,举个例子:
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
function Parent(){
this.names = ['缪克立', '缪立克'];
}

function Child(){
this.age = [18]
}

Child.prototype = new Parent();

var child1 = new Child();

child1.names.push('mkl');
child1.age.push(19)

console.log(child1.names); //['缪克立', '缪立克', 'mkl']
console.log(child1.age) //[18, 19]


var child2 = new Child();

console.log(child2.names); //['缪克立', '缪立克', 'mkl']
console.log(child2.age); //[18]


console.log(Child) //Child(){this.age = [18]}
console.log(Child.prototype) //['缪克立', '缪立克', 'mkl']

child1child2都是new的两个新的对象,双方都有各自的age属性和names属性,但是它们的原型都是Parent即定义成了一个对象,当修改child1names属性时,由于child1没有这个属性,就往原型上找,于是找到了Parent,并且Parent是引用类型,child1保存的原型就是parent的地址值,所以修改names属性就顺着地址值找到Parentnames属性更改。child1child2age属性是基本数据类型,双方保存的都是原本的值,所以没有共享。

  1. 在创建Child的实例时,不能向Parent传参

2. 借用构造函数(经典继承)

废话不多说,直接上代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function Parent(){
this.names = ['缪克立', '缪立克'];
}


function Child(){
Parent.call(this);
}

var child1 = new Child();

child1.names.push('mkl');

console.log(child1.names);

var child2 = new Child();

console.log(child2.names);

上面的Parent.call(this)就是借用构造函数的调用。通过call()apply()方法,Parent构造函数在为Child的实例创建的新对象的上下文中执行了。相当于在新的Child对象上运行了Parent()函数中的所有初始化代码。结果就是每个child实例都会有自己的names属性。

优点:

  1. 避免了引用类型的属性被所有实例共享。

  2. 可以在Child中向Parent传参。

    举个例子:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    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

缺点

方法都在构造函数中定义,每次创建实例都会创建一遍方法。

3. 组合继承

结合原型链继承和经典继承。

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
30
31
function Parent(name){
this.name = name;
this.colors = ['red','blue','green'];
}

Parent.prototype.getName = function(){
return 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); // kevin
console.log(child1.age); // 18
console.log(child1.colors); //['red', 'blue', 'green', 'black']
console.log(child1.getName()) // kevin
var child2 = new Child('daisy', '20')
console.log(child2.name); // daisy
console.log(child2.age); // 20
console.log(child2.colors);//['red', 'blue', 'green']
console.log(child2.getName()) // daisy

通过这种方式就可以创建两个child实例,让这两个实例都有自己的属性,包括colors ,同时还共享相同的方法,弥补了借用构造函数继承的缺点,实现函数服用。

优点:融合原型链继承和经典继承的优点,是JavaScript中最常用的继承模式。

4. 原型式继承

1
2
3
4
5
function createObj(o){
function F(){}
F.prototype = o;
return new F();
}

实际上就是 ES5 Object.create的模拟实现,将传入的对象作为创建的对象的原型。

缺点:包含引用类型的属性值始终都会共享相同的值,这点跟原型链继承一样。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function createObj(o){
function F(){}
F.prototype = o;
return new F();
}
var person = {
name: 'kevin',
friends: ['daisy', 'kelly']
}

var person1 = createObj(person)
var person2 = createObj(person)


person1.name = 'person1';
console.log(person2.name) //kevin

person1.friends.push('taylor');
console.log(person2.friends); //['daisy', 'kelly', 'taylor']

注意:修改person1.name的值,person2.name的值并未发生改变,并不是因为person1person2有独立的name值,而是因为person1.name = 'person1',给person1添加了name值,并非修改了原型上的name值。

5. 寄生式继承

创建一个仅用于封装继承过程的函数,该函数在内部以某种形式来做增强对象,最后返回对象。

1
2
3
4
5
6
7
function createObj (o) {
var clone = Object.create(o);
clone.sayName = function () {
console.log('hi');
}
return clone;
}

缺点:跟借用构造函数模式一样,每次创建对象都会创建一遍方法。

6.寄生组合式继承

重复一下组合继承的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function Parent(name){
this.name = name;
this.colors = ['red','blue','green'];
}

Parent.prototype.getName = function(){
return 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);

组合继承最大的缺点是会嗲用两次父构造函数。

一次是设置子类型实例的原型的时候:

1
Child.prototype = new Parent();

一次在创建子类型实例的时候:

1
var child1 = new Child('kevin', '18')

回想一下new的模拟实现,在这句中,我们就会执行:

1
Parent.call(this.name);

在这里,我们又会调用了以此Parent构造函数。

所以在这里例子中,如果我们打印child1对象,我们会发现child1.prototypechild1都有一个属性为colors,属性值为['red','blue','green']

那么该如何避免这一次重复调用呢?

如果不适用Child.prototype = new Parent(),而是间接的让Child.prototype访问到Parent.prototype呢?

以下代码:

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();


var child1 = new Child('kevin', '18');

console.log(child1);

最后封装一下这个继承方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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。开发人员普遍认为寄生组合式继承是引用类型最理想的继承范式。