继承

89 阅读2分钟

JavaScript继承的多种方式

1. 原型链继承

function Parent () {
  this.name = 'kevin'
}
Parent.prototype.getName = function () {
  console.log(this.name)
}
function Child () {}
Child.prototype = new Parent()
let child = new Child()
child.getName() // kevin

存在的问题

  1. 引用数据类型会被所有创建出来的实例共享

    function Parent () {
      this.names = ['kevin', 'daisy']
    }
    function Child () {}
    Child.prototype = new Parent()
    let child1 = new Child()
    child1.names.push('huhu')
    let child2 = new Child()
    console.log(child2.names) // ['kevin', 'daisy', 'huhu']
    
  2. 在创建Child实例的时候,不能对Parent进行传参

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

function Parent () {
  this.names = ['kevin', 'daisy']
}
function Child () {
  Parent.call(this)
}
let child1 = new Child()
child1.names.push('huhu')
console.log(child1.names) // ['kevin', 'daisy', 'huhu']
let child2 = new Child()
console.log(child2.names) // ['kevin', 'daisy']

优点

  1. 避免引用数据类型被所有创建出来的实例共享

  2. 可以在Child中向Parent进行传参

    function Parent (name) {
      this.name = name
    }
    function Child (name) {
      Parent.call(this, name)
    }
    let child1 = new Child('kevin')
    console.log(child1.name) // kevin
    let child2 = new Child('daisy')
    console.log(child2.name) // daisy
    

缺点

  1. 方法都在构造函数中定义,每次创建实例,都会创建一遍方法

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
let 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"]
let child2 = new Child('daisy', 20)
console.log(child2.name); // daisy
console.log(child2.age); // 20
console.log(child2.colors); // ["red", "blue", "green"]

优点

  1. 融合原型链继承和构造函数的优点,是JavaScript中最常用的继承

4. 原型式继承

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

function createObj(o) {
  function F() {}
  F.prototype = o
  return new F()
}
let obj = createObj({name: 'kevin'})
console.log(obj.name) // kevin

缺点

  1. 跟原型链继承一样,当遇到引用数据类型的时候,数据会被所有创建出来的实例共享

    let person = {
      name: "kevin",
      friends: ["daisy", "kelly"]
    }
    ​
    let person1 = createObj(person)
    let person2 = createObj(person)
    ​
    person1.name = 'person1'
    console.log(person2.name) // kevin
    ​
    person1.friends.push("huhu")
    console.log(person2.friends) // ["daisy", "kelly", "huhu"]
    

    注意: person1.name = 'person1' 是直接添加在 person1 上面的,而不是去修改 person1 的原型。

5. 寄生式继承

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

function createObj (o) {
  var clone = Object.create(o);
  clone.sayHi = function () {
    console.log('Hi');
  }
  return clone;
}
let obj = createObj({})
obj.sayHi() // hi

缺点

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

6. 寄生组合式继承(后期继续补充)

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

一次是在设置子类型实例的原型的时候 Child.prototype = new Parent()

一次是在设置子类型实例的时候 let child1 = new Child('kevin', 18)