JavaScript 沒有真正的 class 繼承,只能透過原型鏈與建構函式呼叫來「模擬」。這個限制催生了多種寫法,各有適用場景。本文把三種常見做法攤開比較。
// 父類
function ParentClass() {
this.className = "ParentClass";
this.auth = "Auth";
this.version = "V1.0";
this.parentClassInfo = function () {
return this.className + "\n" + this.auth + "\n" + this.version;
};
} // 子類
function ChildClassByPrototype() {
this.date = "2013-07-26";
this.classInfo = function () {
return this.parentClassInfo() + "\n" + this.date;
};
}
ChildClassByPrototype.prototype = new ParentClass();
var cctest1 = new ChildClassByPrototype();
cctest1.parentClassInfo();
cctest1.classInfo();把父類的實例指派給子類的 prototype,子類就能使用父類的方法與屬性。原理是原型鏈的向上查找:呼叫 cctest1.parentClassInfo() 時,引擎先在 child 實例上找,找不到就往 ChildClassByPrototype.prototype 找,而那是 ParentClass 的實例,上面有這個方法,於是查找結束。
缺點:父類的屬性是掛在原型上的共享值,而且執行 new ParentClass() 會跑一次父類建構程序,產生意外的副作用。
function ChildClassByApply() {
ParentClass.apply(this, new Array());
this.date = "2013-07-26";
this.classInfo = function () {
return this.parentClassInfo() + "\n" + this.date;
};
}apply 可以理解為「用 A 方法替換 B 方法」:第一個參數是 B 的 this 對象,第二個參數是傳給 A 的參數陣列。若無參數,傳 [] 或 new Array() 都可以。
缺點:它只借用建構程序,並未建立原型鏈。用 instanceof 檢測時,子類實例不會被判定為 ParentClass 的實例。
function ChildClassByCall() {
ParentClass.call(this, arguments);
this.date = "2013-07-26";
this.classInfo = function () {
return this.parentClassInfo() + "\n" + this.date;
};
}
ChildClassByCall.prototype = new ParentClass();call 與 apply 作用相同,只是參數不用包成陣列,而是逐一傳入。加上原型指派這一行之後,方法替換與屬性複製都到位了——這正是單用 call 不足之處。
function ParentClass(className, auth, version) {
this.className = className;
this.auth = auth;
this.version = version;
this.parentClassInfo = function () {
return this.className + "\n" + this.auth + "\n" + this.version;
};
}這種情況下 prototype 寫法就不適用了(你無法在指派原型時傳入正確參數),得改用 apply 或 call:
function ChildClassByApply(className, auth, version) {
ParentClass.apply(this, [className, auth, version]);
this.date = "2013-07-26";
this.classInfo = function () {
return this.parentClassInfo() + "\n" + this.date;
};
}
function ChildClassByCall(className, auth, version) {
ParentClass.call(this, arguments[0], arguments[1], arguments[2]);
this.date = "2013-07-26";
this.classInfo = function () {
return this.parentClassInfo() + "\n" + this.date;
};
}
ChildClassByCall.prototype = new ParentClass();
var cctest2 = new ChildClassByApply("ParentClass", "Auth", "V1.0");
var cctest3 = new ChildClassByCall("ParentClass", "Auth", "V1.0");在 OO 的語意裡,子類應該「也是」父類的一種。用 instanceof 檢測會發現:apply 寫法的子類並非 ParentClass 實例,而 call + prototype 則可以。因此在需要維持型別關係時,call + prototype 是三者中最穩妥的。
ES5 之後有了更好的方案,可以避免真的執行一次父類建構函式:
function inherit(Child, Parent) {
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child; // 補回 constructor 指向
}
function ChildClass(className, auth, version) {
ParentClass.call(this, className, auth, version);
this.date = "2013-07-26";
}
inherit(ChildClass, ParentClass);這段就是所謂的「原型中繼」模式,也是先前那些繼承庫的核心做法。而到了 ES6,class 與 extends 語法糖讓這一切變成一行:class ChildClass extends ParentClass {},底層仍然是原型鏈,但可讀性完全不同。新專案請直接用 ES6 class;理解上述三種寫法,則是為了讀懂既有程式與掌握原型機制的必經之路。