javascript深入之继承(笔记4)

1 原型链继承

function Parent(){

   this.name='aaa'

}

Parent.prototype.getName=function(){

   console.log(this.name)

}

function Child(){

}

Child.prototype=new Parent();

var child1=new Child();

console.log(child1.getName());//aaa

问题1:引用类型的属性被所有实例共享,例

function Parent(){

   this.name=['aaa','bbb'];

}

function Child(){

 

}

Child.prototype=new Parent();

var child1=new Child();

child1.name.push('ccc');

console.log(child1);

console.log(child1.name);//["aaa","bbb","ccc"]

var child2=new Child();

console.log(child2);//自己打印看看

console.log(child2.name);//["aaa","bbb","ccc"]

在创建Child的实例时,不能像Parent传参

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

function ParentP(){

   this.names=['aaa','bbb'];

}

function Parent(){

  ParentP.call(this);

}

var person1=new Person();

person1.name.push('ccc');

console.log(person1.name);//["aaa","bbb","ccc"]

var person2=new Person();

console.log(person2.name);//["aaa","bbb"]

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

可以在Parent中向ParentP中传参

如果传参的话,方法都在构造函数中定义,每一创建实例都会创建一遍方法

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); // 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 中最常用的继承模式。

4原型式继承

function createObj(o){

   function F(){}

   F.prototype=o;

   return new F();

}

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

缺点:

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

5 寄生式继承

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

function createObj(o){

   var clone=Object.create(o);

   clone.sayName=function(){

      console.log('cfm');

   }

   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('cfm',21);

console.log(child1);

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

一次式设置子类型的原型的时候:

Child.prototype=new Parent();

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

var child1=newe Child('cfm',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。开发人员普遍认为寄生组合式继承是引用类型最理想的继承范式。

 

 

参考资源

https://github.com/mqyqingfeng/Blog/issues/16  很棒

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值