JS繼承的幾種方式

首先定義一個父類

// 定義一個動物類
function Animal (name) {
  // 屬性
  this.name = name || 'Animal';
  // 實例方法
  this.sleep = function(){
    console.log(this.name + '正在睡覺!');
  }
}

// 原型方法
Animal.prototype.eat = function(food) {
  console.log(this.name + '正在吃:'+ food);
};

1、原型鏈繼承

核心: 將父類的實例作為子類的原型

function Cat() {}
cat.prototype = new Animal()
cat.prototype.name = "cat"

var cat = new Cat();
console.log(cat.name)
console.log(cat.eat('fish'))
console.log(cat.sleep())
console.log(cat instanceof Animal)  // true
console.log(cat instanceof Cat) // true
  • 特點:

    1. 純粹的繼承關(guān)系,實例是子類的實例,也是父類的實例
    2. 父類新增原型方法/原型屬性,子類都能訪問到
    3. 簡單,易于實現(xiàn)
  • 缺點:

    1. 可以在Cat構(gòu)造函數(shù)中,為Cat實例增加實例屬性。如果要新增原型屬性和方法,則必須放在new Animal()這樣的語句之后執(zhí)行。
    2. 無法實現(xiàn)多繼承
    3. 來自原型對象的所有屬性被所有實例共享
    4. 創(chuàng)建子類實例時,無法向父類構(gòu)造函數(shù)傳參

2、構(gòu)造繼承

核心:使用父類的構(gòu)造函數(shù)來增強子類實例,等于是復制父類的實例屬性給子類(沒用到原型)

function Cat(name){
  Animal.call(this); 
  this.name= name || 'Tom';
}
// Test Code
var cat = new Cat();
console.log(cat.name);
console.log(cat.sleep());
console.log(cat instanceof Animal);   //false
console.log(cat instanceof Cat); // true
  • 特點:

    1. 解決了1中,子類實例共享父類引用屬性的問題
    2. 創(chuàng)建子類實例時,可以向父類傳遞參數(shù)
    3. 可以實現(xiàn)多繼承(call多個父類對象)
  • 缺點:

    1. 實例并不是父類的實例,只是子類的實例
    2. 只能繼承父類的實例屬性和方法,不能繼承原型屬性/方法
    3. 無法實現(xiàn)函數(shù)復用,每個子類都有父類實例函數(shù)的副本,影響性能

3、實例繼承

核心:為父類實例添加新特性,作為子類實例返回

function Cat(){ 
  var instance = new Animal();
  instance.name = name || 'Tom'; 
  return instance;
} 
// Test Code
var cat = new Cat();
console.log(cat.name);
console.log(cat.sleep());
console.log(cat instanceof Animal); // true
console.log(cat instanceof Cat); // false
  • 特點:

    1. 不限制調(diào)用方式,不管是new子類()還是子類(),返回的對象具有相同的效果
  • 缺點:

    1. 實例是父類的實例,不是子類的實例
    2. 不支持多繼承

4、拷貝繼承

function Cat(name){ 
  var animal = new Animal();
  // 遍歷拷貝屬性 
  for(var p in animal){
    Cat.prototype[p] = animal[p];
  }
  Cat.prototype.name = name || 'Tom';
} 
// Test Code
var cat = new Cat();
console.log(cat.name);
console.log(cat.sleep());
console.log(cat instanceof Animal); // false
console.log(cat instanceof Cat); // true
  • 特點:

    1. 支持多繼承
  • 缺點:

    1. 效率較低,內(nèi)存占用高(因為要拷貝父類的屬性)
    2. 無法獲取父類不可枚舉的方法(不可枚舉方法,不能使用for in訪問到)

5、組合繼承

核心:通過調(diào)用父類構(gòu)造,繼承父類的屬性并保留傳參的優(yōu)點,然后通過將父類實例作為子類原型,實現(xiàn)函數(shù)復用

function Cat(name){
  Animal.call(this); 
  this.name = name || 'Tom';
}
Cat.prototype = new Animal(); //修復構(gòu)造函數(shù)指向
Cat.prototype.constructor = Cat; // Test Code
var cat = new Cat();
console.log(cat.name);
console.log(cat.sleep());
console.log(cat instanceof Animal); // true
console.log(cat instanceof Cat); // true
  • 特點:

    1. 彌補了方式2的缺陷,可以繼承實例屬性/方法,也可以繼承原型屬性/方法
    2. 既是子類的實例,也是父類的實例
    3. 不存在引用屬性共享問題
    4. 可傳參
    5. 函數(shù)可復用
  • 缺點:

    1. 調(diào)用了兩次父類構(gòu)造函數(shù),生成了兩份實例(子類實例將子類原型上的那份屏蔽了)

6、寄生組合繼承

核心:通過寄生方式,砍掉父類的實例屬性,這樣,在調(diào)用兩次父類的構(gòu)造的時候,就不會初始化兩次實例方法/屬性,避免的組合繼承的缺點

function Cat(name){
  Animal.call(this); 
  this.name = name || 'Tom';
}
(function(){ 
  // 創(chuàng)建一個沒有實例方法的類
  var Super = function(){};
  Super.prototype = Animal.prototype; //將實例作為子類的原型
  Cat.prototype = new Super();
})(); 
// Test Code
var cat = new Cat();
console.log(cat.name);
console.log(cat.sleep());
console.log(cat instanceof Animal); // true
console.log(cat instanceof Cat); //true
Cat.prototype.constructor = Cat; // 需要修復下構(gòu)造函數(shù)

或者

function Cat(name){
  Animal.call('this');
  this.name = name || 'Tom';
}
Cat.prototype = Object.create(Animal.prototype, {
  constructor: {
    value: Cat,
    enumerable: false,
    writable: true,
    configurable: true
  }
})
// Test Code
var cat = new Cat();
console.log(cat.name);
console.log(cat.sleep());
console.log(cat instanceof Animal); // true
console.log(cat instanceof Cat); //true
<br>
// 以上繼承實現(xiàn)的核心就是將父類的原型賦值給了子類,并且將構(gòu)造函數(shù)設(shè)置為子類,這樣既解決了無用的父類屬性問題,還能正確的找到子類的構(gòu)造函數(shù)。
  • 特點:

    1. 堪稱完美
  • 缺點:

    1. 實現(xiàn)較為復雜

7、Class 繼承

在 ES6 中,我們可以使用 class 去實現(xiàn)繼承,并且實現(xiàn)起來很簡單

核心: 使用 extends 表明繼承自哪個父類,并且在子類構(gòu)造函數(shù)中必須調(diào)用 super,這段代碼可以看成 Animal.call(this, name)Class 的本質(zhì)就是函數(shù)

class Cat extends Animal {
  constructor(name){
    super(name) 
    this.name= name || 'Animal';
  }
}
var cat = new Cat();
console.log(cat.name);
console.log(cat.sleep());
console.log(cat instanceof Animal); // true
console.log(cat instanceof Cat); //true
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容