JavaScript继承的几种方式

1、继承原理

原型链
不知道什么是原型链?
来读几个关键词:函数

哥欧 构 构造函数 构造函数 构造函数  
实例 实例 实例  
原型对象 原型对象 原型对象
prototype prototype prototype
__proto__ __proto__ __proto__
constructor constructor constructor
instanceof instanceof instanceof

实现instanceof

function newInstanceof (left, right) {
  let leftProto = left.__proto__;
  let rightPrototype = right.prototype
  while(true) {
    if(leftProto === rightPrototype) {
      return true;
    } else if (leftProto === null) {
      return false;
    } else {
      leftProto = leftProto.__proto__
    }
  }
}

2、继承方法

一、构造函数

function FatherA() {
    this.money = 100000;
  }

  function ChildA() {
    FatherA.call(this);
    this.soliloquy = '开心';
  }
  var childAng = new ChildA();
  FatherA.prototype.makeMoneyWay = function () {
    console.log('next lottery number is ……');
  }

假设某地有个习俗,FatherA会给ChildA准备10万教育基金,
构造函数继承能够完美的让孩子获取到准备好的教育基金。
然而这个时候FatherA新领悟到了一串神秘代码,childAng却用不了性能

childAng.makeMoneyWay(); // 会报错

这样的继承感受不是亲身的。ui

二、原型链继承

function FatherB() {
    this.money = 100000;
    this.card = ['工商', '建设'];
  }

  function ChildB() {
    this.soliloquy = '开心';
  }
  ChildB.prototype = new FatherB()
  var childBobo = new ChildB();
  var childBigShuan = new ChildB();
  FatherB.prototype.makeMoneyWay = function () {
    console.log('next lottery number is ……');
  }
  childBobo.card.push('招商'); // 引用类型受影响
  childBobo.money += 1000; // 值类型在此不受影响

原型链继承解决了构造函数的问题,FatherB上新的技能,ChildB的实例也能很好的使用。
图示:
图片描述this

然而一个新问题就是,当实例有多个例如childBobo(波波)和childBigSuan(大栓)。
波波这个时候新办了一张银行卡,结果大栓立马能用,这样让波波以为很没有隐私,哦不,是很不公平,凭啥你刷个人卡。spa

三、组合方式继承

function FatherC() {
    this.money = 100000;
    this.card = ['工商', '建设'];
  }

  function ChildC() {
    FatherC.call(this);
    this.soliloquy = '开心';
  }
  ChildC.prototype = new FatherC()
  var childCuifa = new ChildC();
  var childChizi = new ChildC();
  FatherC.prototype.makeMoneyWay = function () {
    console.log('next lottery number is ……');
  }
  childCuifa.card.push('招商');
  childCuifa.money += 1000;

出了问题总要解决,这时候出现了由构造函数和原型链的组合继承,这样既让ChildC实例的数据相互独立,也让ChildC能获取到FatherC的新技能。
社会问题是解决了。新的问题就是性能问题,这类继承会调用FatherC两次。显然用的越多,消耗的资源越多。(虽然如今硬件很发达,可是1我的睡100平米的床终究仍是不方面,起夜还没下床呢就尿了)prototype

四、组合方式继承进阶版

function FatherD() {
    this.money = 100000;
    this.card = ['工商', '建设'];
  }

  function ChildD() {
    FatherD.call(this);
    this.soliloquy = '开心';
  }
  ChildD.prototype = FatherD.prototype
  var childDazhu = new ChildD();
  var childDabing = new ChildD();
  console.log('分不清是孩子仍是父辈', childDazhu instanceof ChildD, childDazhu instanceof FatherD);
  console.log('大柱的构造函数指向的是ParentD', childDazhu.constructor);

上一个组合继承中code

ChildC.prototype = new FatherC()

这行代码主要是为了让ChildC.prototype与new FatherC()的__proto__在一条原型链上。
因此换个思路,new FatherC()的__proto__也就是FatherC.protorype。
图示:
图片描述对象

替换后,既不会再执行FatherC,也让原型链合并到了一块儿。
这个方案的隐藏问题就是,childDazhu(大柱)这个实例,会让人不知道是孩子仍是爸爸。blog

五、组合方式继承终极版

function FatherE() {
    this.money = 100000;
    this.card = ['工商', '建设'];
  }

  function ChildE() {
    FatherE.call(this);
    this.soliloquy = '开心';
  }
  ChildE.prototype = Object.create(FatherE.prototype);
  ChildE.prototype.constructor = ChildE;
  var childErgo = new ChildE();
  var childErwa = new ChildE();
  var fatherErjun = new FatherE();
  console.log('能分清是孩子仍是父辈', childErgo instanceof ChildE, childErgo instanceof FatherE);
  console.log('二狗的构造函数已指向childE', childErgo.constructor);

图示:
图片描述继承

关键代码

Object.create

Object.create建立对象的方法就是用原型链来链接的。
Object.create()方法建立一个新对象,使用现有的对象来提供新建立的对象的__proto__。
参考自:
https://developer.mozilla.org...var a = {name:'a'};var b = Object.create(a);b.__proto__ === a; // true这样ChildE与FatherE.prototype就在一条链上面了,而且数据也进行了隔离,此时修改ChildE原型对象的构造函数指向,不会影响到FatherE的原型对象。也让childErgo(二狗)有了肯定的归属。幸福的结局!!

相关文章
相关标签/搜索