1.原型链继承

function Parent() {
  this.name = "xieyezi"
}

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

function Child() {}

Child.prototype = new Parent()

var child = new Child()

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

缺点: 1.引用类型的属性会被所有实例共享,例如:

function Parent() {
  this.names = ["xieyezi"]
}

function Child() {}

Child.prototype = new Parent()

var child1 = new Child()

child1.names.push("juefei")

console.log(child1.names) // ["xieyezi", "juefei"]

var child2 = new Child()

console.log(child2.names) // ["xieyezi", "juefei"]

2.在创建 Child 实例时,不能向 Parent 传参

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

function Parent() {
  this.names = ["xieyezi"]
}

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

var child1 = new Child()

child1.names.push("juefei")

console.log(child1.names) // ["xieyezi", "juefei"]

var child2 = new Child("babad")

console.log(child2.names) // ["xieyezi"]

优点: 1.避免了引用类型的属性被所有实例共享; 2.可以在 Child 中向 Parent 传参,例如:

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

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

var child1 = new Child("xieyezi")

console.log(child1.name) // xieyezi

var child2 = new Child("juefei")

console.log(child2.name) // juefei

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

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("xieyezi", "25")

child1.colors.push("black")

console.log(child1.name) // xieyezi
console.log(child1.age) // 25
console.log(child1.colors) // ["red", "blue", "green", "black"]

var child2 = new Child("juefei", "24")

console.log(child2.name) // juefei
console.log(child2.age) // 24
console.log(child2.colors) // ["red", "blue", "green"]

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

4.原型式继承

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

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"]

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

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

5. 寄生式继承

function createObj(o) {
  var clone = Object.create(o)
  clone.sayName = function () {
    console.log("hi")
  }
  return clone
}

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

缺点:跟借用构造函数模式一样,每次创建对象都会创建一遍方法。

6. 寄生组合式继承

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

function Parent(value) {
  this.val = value
}

Parent.prototype.getValue = function () {
  console.log(this.val)
}

function Child(value) {
  Parent.call(this, value)
}

Child.prototype = object(Parent.prototype)
// 上面这句等价于 `Child.prototype = Object.create(Parent.prototype)`

相比于组合继承,寄生组合式继承调用两次父构造函数的缺点。