javaScript之继承

这里是new的模拟代码

1
2
3
4
5
6
7
8
9
10

function Mynew(){
var obj = new Object();
Constructor = [].shift.call(arguments,0);
obj.__proto__ = Constructor.prototype;

var ret = Constructor.apply(obj, arguments);

return typeof ret === 'object' ? ret : obj;
}

原型的继承

1
2
3
4
5
6
7
8
9
10
function Parent(){
this.name = 'blue'
}
Parent.prototype.getName = function () {
console.log(this.name)
}
function Child() {}

var child1 = Mynew(Parent)
child1.getName() //blue

使用Mynew来模拟new操作时有个问题

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.name = ['blue']
}
function Child() {}
function Child2() {}

var s = new Parent()
var t = Mynew(Parent)

// console.log("t--- ",t)
// console.log("s--- ",s)

Child.prototype = t
Child2.prototype = s

var child1 = Mynew(Child)
var child2 = new Child2()
var child3 = new Child()


console.log(child1) //Parent {} ???这为什么是Parent而不是Child {}
console.log(child2) //Child2 {}
console.log(child3) //Child {}

console.log(child1.name) //["blue"]
console.log(child2.name) //["blue"]
console.log(child3.name) //["blue"]

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
function Parent () {
this.names = ['kevin', 'daisy'];
this.age = 10
}

function Child () {

}

Child.prototype = new Parent();

var child1 = new Child();

child1.names.push('yayu');
child1.age = 100;

console.log(child1.names); // ["kevin", "daisy", "yayu"]
console.log(child1.age); // 100

var child2 = new Child();
// 这继承了上面修改的数据,说明引用类型是可以被实例修改和共享的,
console.log(child2.names); // ["kevin", "daisy", "yayu"]
// 非引用的属性不会被实例修改
console.log(child2.age); // 10

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

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

function Child () {
// 类似 super()的用法
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"]

优点:

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

  • 可以在 Child 中向 Parent 传参

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

    function Child (name) {
    //向Parent传递参数
    Parent.call(this, name);
    }

    var child1 = new Child('kevin');

    console.log(child1.name); // kevin

    var child2 = new Child('daisy');

    console.log(child2.name); // daisy

    缺点:

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

    组合继承(最常见的继承方式)

    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
    32
    33
    34
    35
    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原型指向Parent
    Child.prototype = new Parent();
    // 修正Child原型的构造函数的指向
    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"]

    var child2 = new Child('daisy', '20');

    console.log(child2.name); // daisy
    console.log(child2.age); // 20
    console.log(child2.colors); // ["red", "blue", "green"]

寄生组合式继承

之前的组合继承最大缺点就是会调用两次父构造函数
一次是设置子类型实例的原型的时候
Child.prototype = new Parent()
一次是在创建子类型实例的时候:
var child1 = new Child('kevin', 18)
根据new的模拟实现代码可以看到,我们还会执行类似下面的代码

1
2
//在模拟代码中是 obj.apply(this, arguments)
Parent.call(this, name)

这里相当于我们又调用了一次Parent构造函数

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

1
2
3
4
5
6
// 关键的三步, 这里就是上面设想的实现代码
var F = function () {};

F.prototype = Parent.prototype;

Child.prototype = new F();

完整代码

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
  function object(o) {
function F() {}
F.prototype = o;
return new F();
}
// child的原型指向parent的原型
function prototype(child, parent) {
var prototype = object(parent.prototype);
//child.prototype = F.prototype = parent.prototype
prototype.constructor = child;
child.prototype = prototype;
}

// 当我们使用的时候:
function Parent (name) {
this.name = name;
this.colors = ['red', 'blue', 'green'];
}
function Child(name) {
Parent.call(this,name)
}

prototype(Child, Parent);
var child = new Child('blue')
// console.log(Child.prototype)
console.log(child.name) // blue