繼承是物件導向的核心課題,JavaScript 因為沒有 class 語法(ES6 之前),衍生出多種模擬手法。以下整理三種常見寫法,並討論各自的取捨。
第一種:prototype 原型鏈
// 父類
function person(){
this.hair = 'black';
this.eye = 'black';
this.skin = 'yellow';
this.view = function(){
return this.hair + ',' + this.eye + ',' + this.skin;
}
}
// 子類
function man(){
this.feature = ['beard','strong'];
}
man.prototype = new person();
var one = new man();
console.log(one.feature); // ['beard','strong']
console.log(one.hair); // black
console.log(one.eye); // black
console.log(one.skin); // yellow
console.log(one.view()); // black,black,yellow
寫法最為簡潔:把子類的 prototype 指向父類的一個實例,子類就能直接沿用父類的方法。
這裡要理解 prototype 是什麼。每個由 function 定義出來的物件都帶有一個預設的原型屬性,它是一個物件型別,作用是支撐屬性的「向上鏈式查找」:當某個屬性在當前物件上找不到,JavaScript 就會沿著 prototype 所指向的物件繼續找;若還找不到,就一路往上一層的 prototype 找,直到命中或供應鏈末端為 undefined 為止。
以前例的 one.view() 為例:先在 one 自身查找 view,沒有;轉向 man.prototype,它的值是 person 的一個實例,而該實例上有 view,於是呼叫成功。
第二種:apply 借用構造函數
// 父類
function person(){
this.hair = 'black';
this.eye = 'black';
this.skin = 'yellow';
this.view = function(){
return this.hair + ',' + this.eye + ',' + this.skin;
}
}
// 子類
function man(){
// person.apply(this, new Array());
person.apply(this, []);
this.feature = ['beard','strong'];
}
var one = new man();
console.log(one.feature); // ['beard','strong']
console.log(one.hair); // black
console.log(one.eye); // black
console.log(one.skin); // yellow
console.log(one.view()); // black,black,yellow
在子類建構時,用 apply 把父類的 this 換成當前物件執行一次,屬性就被複製過來了。
注意一個小細節:如果 apply 不需要傳任何參數,要用 new Array() 或 [] 來占位,傳 null 是無效的。
第三種:call + prototype 組合
// 父類
function person(){
this.hair = 'black';
this.eye = 'black';
this.skin = 'yellow';
this.view = function(){
return this.hair + ',' + this.eye + ',' + this.skin;
}
}
// 子類
function man(){
// person.apply(this, new Array());
person.call(this, []);
this.feature = ['beard','strong'];
}
man.prototype = new person();
var one = new man();
console.log(one.feature); // ['beard','strong']
console.log(one.hair); // black
console.log(one.eye); // black
console.log(one.skin); // yellow
console.log(one.view()); // black,black,yellow
相比第二種,這裡多了一行 man.prototype = new person();。原因是 call 只完成了方法的替換,並沒有複製物件的屬性;加上原型賦值,才能同時取得方法與屬性。Google Map API 當年就採用這種繼承方式。
假設父類改成需要接收參數:
// 父類
function person(hair, eye, skin){
this.hair = hair;
this.eye = eye;
this.skin = skin;
this.view = function(){
return this.hair + ',' + this.eye + ',' + this.skin;
}
}
這時若希望建立子類物件時能順便把參數透傳給父類,單靠 prototype 就不夠了,必須改用 apply 或 call:
// apply 方式
function man(hair, eye, skin){
person.apply(this, [hair, eye, skin]);
this.feature = ['beard','strong'];
}
// call 方式
function man(hair, eye, skin){
person.call(this, hair, eye, skin);
this.feature = ['beard','strong'];
}
但 apply/call 這條路還有一個硬傷,牽涉到 instanceof 運算子。以本例來說,one 除了是 man,語意上也應該是 person;然而純用 apply 繼承之後,one instanceof person 會回傳 false,型別判斷就斷了。
綜合來看,call + prototype 是三者中較均衡的選擇,你可以自己試試 one instanceof BaseClass 是否為 true。不過它仍有缺點:子類建立物件時得把父類所需參數再傳一遍,而且父類的屬性與方法會被重新產生一次,造成不必要的記憶體開銷。
更完善的寫法是用一個中間的空函數做原型橋接,避免直接呼叫父類構造函數:
function Person(name){
this.name = name;
}
Person.prototype.getName = function() {
return this.name;
};
function Chinese(name, nation) {
Person.call(this, name);
this.nation = nation;
}
// 繼承方法
function inherit(subClass, superClass) {
function F() {}
F.prototype = superClass.prototype;
subClass.prototype = new F();
subClass.prototype.constructor = subClass.constructor;
}
inherit(Chinese, Person);
Chinese.prototype.getNation = function() {
return this.nation;
};
var p = new Person('shijun');
var c = new Chinese("liyatang", "China");
console.log(p); // Person {name: "shijun", getName: function}
console.log(c); // Chinese {name: "liyatang", nation: "China", ...}
console.log(p.constructor); // function Person(name){}
console.log(c.constructor); // function Chinese(){}
console.log(c instanceof Chinese); // true
console.log(c instanceof Person); // true
這種「借用構造函數繼承屬性、透過原型鏈繼承方法」的思路,就是日後被廣為採用的寄生組合式繼承雛形,也是 ES6 的 class extends 在底層所做的事。