JavaScript基础专题之继承的实现及其优缺点(十)

492 阅读5分钟

根据《JavaScript高级程序设计》(红宝书)继续总结一下继承的方式及优缺点

1.原型链继承

function Parent () {
    this.name = 'chris';
}

Parent.prototype.getName = function () {
    console.log(this.name);
}

function Child () {

}

Child.prototype = new Parent();

var child = new Child();

console.log(child.getName()) // chris

问题:

  1. 对象作为引用类型,存储在堆内存,属性会被所有实例共享,举个例子:
function Parent () {
    this.names = ['chris', 'daisy'];
}

function Child () {

}

Child.prototype = new Parent();

var child1 = new Child();

child1.names.push('james');

console.log(child1.names); // ["chris", "daisy", "james"]

var child2 = new Child();

console.log(child2.names); // ["chris", "daisy", "james"]
  1. 在创建 Child 的实例时,不能向Parent传参

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

function Parent () {
    this.names = ['chris', 'daisy'];
}

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

var child1 = new Child();

child1.names.push('james');

console.log(child1.names); // ["chris", "daisy", "yayu"]

var child2 = new Child();

console.log(child2.names); // ["chris", "daisy"]

优点:

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

  2. 可以在 Child 中向 Parent 传参

举个例子:

function Parent (name) {
    this.name = name;
}

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

var child1 = new Child('chris');

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

var child2 = new Child('daisy');

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

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

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('chris', '18');

child1.colors.push('black');

console.log(child1.name); // chris
console.log(child1.age); // 18
console.log(child1.colors); // ["red", "blue", "green", "black"]

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

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

优点:融合原型链继承和构造函数的优点,是 JavaScript 中最常用的继承模式。

4.原型式继承

ES5 Object.create 方法 :

var person = {
    name: 'chris',
    friends: ['daisy', 'james']
}

var person1 = Object.create(person);
var person2 = Object.create(person);

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

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


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

模拟实现 Object.create

function createObj(o) {
    function F(){}
    F.prototype = o;
    return new F();
}

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

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)

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

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

Child.prototype = new Parent();

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

var child1 = new Child('kevin', '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;
}

// 关键的三步,只获取prototype,不再调用构造函数
var F = function () {};

F.prototype = Parent.prototype;

Child.prototype = new F();


var child1 = new Child('chris', '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);

这种方式的高效率体现它只调用了一次 Parent 构造函数,并且因此避免了在 Parent.prototype 上面创建不必要的、多余的属性。与此同时,原型链还能保持不变;因此,还能够正常使用 instanceof 和 isPrototypeOf。开发人员普遍认为寄生组合式继承是引用类型最理想的继承范式。-- 红宝书

7.Class继承

ES6 class 一直认为是寄生组合式继承的语法糖:

class Parent {
  constructor(name) {
    this.name = name
  }
  sayName() {
    console.log(this.name)
  }
}

class Child extends Parent {
  constructor(name,age) {
    super(name)
    this.age = age
  }
}

let child1 = new Child('chris',18)

我们可以看看经过 Babel 编译后的样子:

"use strict";

function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }

var Parent =
/*#__PURE__*/
function () {
  function Parent(name) {
    this.name = name;
  }

  var _proto = Parent.prototype;

  _proto.sayName = function sayName() {
    console.log(this.name);
  };

  return Parent;
}();

var Child =
/*#__PURE__*/
function (_Parent) {
  _inheritsLoose(Child, _Parent);

  function Child(age) {
    var _this;

    _this = _Parent.call(this, name) || this;
    _this.age = age;
    return _this;
  }

  return Child;
}(Parent);

我们是不是发现 _inheritsLoose 这个函数似曾相识,没错,就是我们的寄生组合式继承。

还有需要注意的是super 这个关键字,super作为函数调用时,代表父类的构造函数。ES6 要求,子类的构造函数必须执行一次super函数,否则就会报错。

class A {}

class B extends A {
  constructor() {
    super();
  }
}

super 既可以当作函数使用,也可以当作对象使用.

class A {
  p() {
    return 2;
  }
}

class B extends A {
  constructor() {
    super();
    console.log(super.p()); // 2
  }
}

let b = new B();
  1. super作为函数调用时,代表父类的构造函数,super虽然代表了父类A的构造函数,但是返回的是子类B的实例,即super内部的this指的是B
  2. super作为对象时,在普通方法中,指向父类的原型对象;在静态方法中,指向父类。
  3. 在子类普通方法中通过super调用父类的方法时,方法内部的this指向当前的子类实例,这点要特别注意,super是调用的父类的方法,但是内部的this指向的确是子类实例。

JavaScript基础专题系列

JavaScript基础专题之原型与原型链(一)

JavaScript基础专题之执行上下文和执行栈(二)

JavaScript基础专题之深入执行上下文(三)

JavaScript基础专题之闭包(四)

JavaScript基础专题之参数传递(五)

JavaScript基础专题之手动实现call、apply、bind(六)

JavaScript基础专题之类数组对象(七)

JavaScript基础专题之实现自己的new Object(八)

JavaScript基础专题之创建对象几种方式及优缺点(九

JavaScript基础专题之继承的实现及其优缺点(十)

如果有错误或者不严谨的地方,还请大伙给予指正。如果这片文章对你有所帮助或者有所启发,还请给一个赞,鼓励一下作者。