JS 繼承歷經多次演進:原型鏈 → 借用構造函數 → 組合繼承 → 寄生組合繼承 → ES6 Class。本文系統梳理各模式原理、優缺點、代碼實現與選型建議,助你徹底掌握 JS 繼承本質。
function Parent(name) {
this.name = name;
this.colors = ['red', 'blue']; // 引用類型共享問題
}
Parent.prototype.sayName = function() { console.log(this.name); };
function Child() {}
Child.prototype = new Parent('Parent'); // 核心:子原型指向父實例
Child.prototype.constructor = Child; // 修正 constructor
var child1 = new Child();
child1.colors.push('green');
var child2 = new Child();
console.log(child2.colors); // ["red", "blue", "green"] - 引用類型共享!
缺點:1) 父構造函數引用類型屬性被所有實例共享。2) 無法向父構造函數傳參。3) 新增原型方法需在 Child.prototype = ... 之後。
function Parent(name) {
this.name = name;
this.colors = ['red', 'blue'];
this.sayName = function() { console.log(this.name); }; // 方法在構造函數內,無法複用
}
function Child(name, age) {
Parent.call(this, name); // 核心:在子構造函數中調用父構造函數,綁定 this
this.age = age;
}
var child1 = new Child('Child1', 18);
var child2 = new Child('Child2', 20);
child1.colors.push('green');
console.log(child2.colors); // ["red", "blue"] - 解決引用共享
child1.sayName(); // Child1
// 缺點:方法無法複用(每個實例創建新函數),無法訪問父原型方法
function Parent(name) {
this.name = name;
this.colors = ['red', 'blue'];
}
Parent.prototype.sayName = function() { console.log(this.name); };
function Child(name, age) {
Parent.call(this, name); // 第二次調用 Parent():實例屬性初始化
this.age = age;
}
Child.prototype = new Parent(); // 第一次調用 Parent():建立原型鏈
Child.prototype.constructor = Child;
Child.prototype.sayAge = function() { console.log(this.age); };
var child1 = new Child('Child1', 18);
child1.sayName(); // Child1 (來自原型)
child1.sayAge(); // 18 (子原型)
child1.colors.push('green'); // 實例自有屬性,不影響其他實例
缺點:調用了兩次父構造函數(一次建立原型鏈,一次初始化實例),父實例屬性在子原型上產生冗餘副本。
function Parent(name) {
this.name = name;
this.colors = ['red', 'blue'];
}
Parent.prototype.sayName = function() { console.log(this.name); };
function Child(name, age) {
Parent.call(this, name); // 僅調用一次:初始化實例屬性
this.age = age;
}
// 核心:寄生式繼承原型
function inheritPrototype(subType, superType) {
var prototype = Object.create(superType.prototype); // 創建父原型副本
prototype.constructor = subType; // 修正 constructor
subType.prototype = prototype; // 賦值給子原型
}
inheritPrototype(Child, Parent);
Child.prototype.sayAge = function() { console.log(this.age); };
// 效果:原型鏈完整、僅調用一次父構造函數、無冗餘屬性、constructor 正確
class Parent {
constructor(name) {
this.name = name;
this.colors = ['red', 'blue'];
}
sayName() { console.log(this.name); }
}
class Child extends Parent {
constructor(name, age) {
super(name); // 必須先調用 super(),相當於 Parent.call(this, name)
this.age = age;
}
sayAge() { console.log(this.age); }
}
const child = new Child('Child', 18);
child.sayName(); // Child
child.sayAge(); // 18
注意:class 本質是函數,繼承機制等同於寄生組合繼承。super 必須在 this 使用前調用。
function Parent(name, age, hobby) {
this.name = name;
this.age = age;
this.hobby = hobby;
}
function Child(name, age, hobby, grade) {
// call: 參數列表展開
Parent.call(this, name, age, hobby);
this.grade = grade;
}
function Teacher(name, age, hobby, subject) {
// apply: 參數數組
Parent.apply(this, [name, age, hobby]);
// 或 arguments 透傳
// Parent.apply(this, arguments);
this.subject = subject;
}
// 區別:
// call(this, arg1, arg2, ...) - 參數逐個傳遞
// apply(this, [arg1, arg2, ...]) - 參數作為數組傳遞
// 適用場景:apply 適合不定參數/arguments 透傳
| 模式 | 原型鏈 | 實例屬性隔離 | 父構造函數調用次數 | 方法複用 | 推薦度 |
|---|---|---|---|---|---|
| 原型鏈 | ✅ | ❌ (引用共享) | 1 | ✅ | ❌ |
| 借用構造函數 | ❌ | ✅ | 1 | ❌ | ❌ |
| 組合繼承 | ✅ | ✅ | 2 | ✅ | ⭐⭐⭐ (經典) |
| 寄生組合 | ✅ | ✅ | 1 | ✅ | ⭐⭐⭐⭐⭐ (ES5 最佳) |
| ES6 Class | ✅ | ✅ | 1 (super) | ✅ | ⭐⭐⭐⭐⭐ (現代標準) |
var arr = [5, 8, 2, 10, 3];
// Math.max 不接受數組,需 apply
Math.max.apply(null, arr); // 10
Math.min.apply(null, arr); // 2
// ES6 展開運算符(更優雅)
Math.max(...arr); // 10
// 注意:展開運算符參數數量受棧大小限制(~10萬),超大數組用 reduce 或分塊
JS 繼承本質是原型鏈委託 + 構造函數盜用。現代項目直接使用 ES6 Class,語義清晰、工具鏈支援完善、底層即最優寄生組合。理解歷史演進過程,有助於閱讀舊代碼、面試應對、邊界場景排查(如多重繼承模擬、Mixin 合成)。核心記住:Object.create 創建純凈原型副本,super 替代 Parent.call(this),constructor 修正不可省略。