深刻学习js之——继承的多种方式和优缺点#13

深刻学习js系列是本身阶段性成长的见证,但愿经过文章的形式更加严谨、客观地梳理js的相关知识,也但愿可以帮助更多的前端开发的朋友解决问题,期待咱们的共同进步。前端

若是以为本系列不错,欢迎点赞、评论、转发,您的支持就是我坚持的最大动力。数组


这篇文章咱们聊聊JavaScript中继承微信

一、原型链继承

// 建立一个构造函数 Parent
function Parent() {
  // 属性name 赋值 kevin
  this.name = "kevin";
}
// 在构造函数的原型对象上添加 getName 方法
Parent.prototype.getName = function() {
  // 打印 name
  console.log(this.name);
};
// 建立 另外一个 构造函数
function Child() {}
// 子类的原型指向父类的实例
Child.prototype = new Parent();

// 建立子类实例
var child1 = new Child();
// 打印 name 属性 kevin
console.log(child1.getName()); // kevin
复制代码

这种写法有什么问题呢? 1.引用类型的属性被全部实例共享,举个例子:闭包

function Parent() {
  // 添加的name属性是一个数组类型 赋值 "kevin", "daisy"
  this.names = ["kevin", "daisy"];
}
// 子类构造函数
function Child() {}
// 将父类的实例对象 赋值给子类的原型对象
Child.prototype = new Parent();
// 实例化子类
var child1 = new Child();
// 子类给name 属性添加 一个元素
child1.names.push("yayu");
console.log(child1.names); // ["kevin", "daisy", "yayu"]
// 再建立一个实例
var child2 = new Child();
console.log(child2.names); // ["kevin", "daisy", "yayu"]
复制代码

上面的例子中,咱们使用原型继承的方式让 child 子类继承 parent 的属性而且父类的属性是一个数组,也就是引用类型。当咱们实例化子类生成一个对象 child1的时候,child1 可以访问父类的 name 属性,而且向 name 属性中添加了一个数据。当咱们再实例化一个子类 child2 时候 这个时候打印 ["kevin", "daisy", "yayu"]这并非咱们想要看到的结果。app

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

function Parent() {
  // 添加引用类型属性
  this.names = ["kevin", "daisy"];
}

// 子类构造函数
function Child() {
  // 内部使用call 执行 Preent 改变 父类的this指向 指向子类
  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"]
复制代码

观察上述代码实现们发现, 这种  继承的方式解决了第一种  继承方式那种引用类型的问题。 一、避免了引用类型被全部的  实例共享。 二、能够  向父类Parent中传参。函数

举个例子:post

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
复制代码

上述代码中,在建立子类实例的时候,向构造函数中传递了 name 参数  经过这个子类的构造函数又传参给了父类的构造函数,从而能够按照需求 进行打印名字。学习

固然这种继承方式也是优缺点的,全部的方法都在构造函数中声明,当咱们实例化对象的 时候,每次都对会调用构造函数,从而从新建立一遍方法。ui

三、组合继承(双剑合璧)

原型链继承和经典继承双剑合璧。this

// 建立 父类构造函数
function Parent(name) {
  // 接收属性 接收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();
// 将子类的原型对象的contructor 属性从新赋值 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"]
复制代码

这种组合继承的实现方式融合了构造函数和 原型链继承 的优势,是被普遍使用的一种继承方式

4.原型式继承

function createObj(o) {
  function F() {}
  F.prototype = o;
  return new F();
}
复制代码

就是 ES5 Object.create 的模拟实现,将传入的对象做为建立的对象的原型。 缺点: 包含引用类型的属性值始终都会共享相应的值,这点跟原型链继承同样。

var person = {
  name: "kevin",
  friends: ["daisy", "kelly"]
};

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

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

person1.firends.push("taylor");
console.log(person2.friends); // ["daisy", "kelly", "taylor"]
复制代码

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

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;
}

// 关键的三步
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。开发人员广泛认为寄生组合式继承是引用类型最理想的继承范式。

深刻学习JavaScript系列目录

欢迎添加个人我的微信讨论技术和个体成长。

欢迎关注个人我的微信公众号——指尖的宇宙,更多优质思考干货

相关文章
相关标签/搜索