JavaScript 不像 Java 那樣對繼承有嚴格明確的定義,它的實作方式如其變數使用風格一樣寬鬆——你可以自己設計一套「模仿」繼承的寫法。以下是四種常見做法。
<script type="text/javascript">
function classA(str) {
this.str = str;
this.printstr = function () {
document.write(this.str);
document.write("<br>");
}
this.getstr = function () {
return this.str;
}
}
function classB(name, str) {
// 下面兩句相當於把 classA 的程式碼搬到這裡
this.newMethod1 = classA;
this.newMethod1(str);
// 注意:新的方法與屬性定義必須在刪除 newMethod 之後,
// 因為可能覆蓋父類的屬性和方法
delete this.newMethod1;
this.name = name;
this.sayName = function () {
document.write(this.name);
document.write("<br>");
}
}
var a = new classB("Amy", "helloworld");
a.printstr();
alert(a.getstr());
a.sayName();
</script>
用 function 定義的程式碼塊相當於一個類,你可以用 this 為它添加屬性和方法。關鍵在這兩句:
this.newMethod1 = classA; this.newMethod1(str);
先在 classB 中宣告 newMethod1 指向 classA,再呼叫它,效果等同於把 classA 區塊內的內容複製過來執行。這樣建立出來的 classB 物件,自然擁有 classA 的屬性與方法。
物件冒充還可以實現多繼承:
function ClassZ() {
this.newMethod = ClassX;
this.newMethod();
delete this.newMethod;
this.newMethod = ClassY;
this.newMethod();
delete this.newMethod;
}
不過 ClassY 會覆蓋 ClassX 中同名的屬性與方法。如果設計得當,ClassZ 本來就不該繼承兩個擁有相同成員的類。
<script type="text/javascript">
function classA(str) {
this.str = str;
this.printstr = function () {
document.write(this.str);
document.write("<br>");
}
this.getstr = function () {
return this.str;
}
}
function classB(name, str) {
// 利用 call 方法實現繼承
classA.call(this, str);
this.name = name;
this.sayName = function () {
document.write(this.name);
document.write("<br>");
}
}
var a = new classB("Amy", "helloworld");
a.printstr();
alert(a.getstr());
a.sayName();
</script>
call() 的第一個參數是要綁定的物件,這裡傳入 this 代表當前物件;後面的參數則是呼叫 classA 時所需的參數。classA.call(this, str) 同樣等同於把 classA 區塊內的程式碼複製過來執行,於是 classB 的物件可以直接使用 classA 的屬性與方法。這種方式比物件冒充更簡潔,也是實務上最常見的寫法之一。
<script type="text/javascript">
function cA() {};
cA.prototype.name = "John";
cA.prototype.sayName = function () {
document.write(this.name);
document.write("<br>");
}
function cB() {};
cB.prototype = new cA();
cB.prototype.age = 23;
cB.prototype.sayAge = function () {
document.write(this.age);
document.write("<br>");
}
var objB = new cB();
objB.sayAge();
objB.sayName();
document.write("is objB the instance of cA " + (objB instanceof cA));
document.write("<br>");
document.write("is objB the instance of cB " + (objB instanceof cB));
document.write("<br>");
</script>
這裡類的定義要用 prototype,定義函數時不帶參數;原型上的屬性與方法相當於 Java 中被 static 修飾的成員,屬於所有物件。特殊之處在於這一句:
cB.prototype = new cA();
它相當於把 cA 物件的內容複製給 cB,之後 cB 還能追加自己的屬性與方法。此時 objB instanceof cA 與 instanceof cB 都會回傳 true,這正是原型鏈的體現。
<script type="text/javascript">
function cA(name) {
this.name = name;
};
cA.prototype.sayName = function () {
document.write(this.name);
document.write("<br>");
}
function cB(name, age) {
cA.call(this, name);
this.age = age;
};
cB.prototype = new cA();
cB.prototype.sayAge = function () {
document.write(this.age);
document.write("<br>");
}
var objB = new cB("Alan", 27);
objB.sayName();
objB.sayAge();
document.write("is objB the instance of cA " + (objB instanceof cA));
document.write("<br>");
document.write("is objB the instance of cB " + (objB instanceof cB));
document.write("<br>");
</script>
這裡把屬性封裝在類體內,方法則用原型方式定義,是相當實用的一種組合。需要注意兩點:cB 類體內有 cA.call(this, name),同時還要把 cB 的原型指向 cA 物件,即 cB.prototype = new cA();。前者相當於複製 cA 類體內的程式碼,後者把 cA 的方法掛給 cB,同時 cB 仍可追加自己的成員。
這也是業界公認比較理想的組合式繼承寫法:用 call 繼承屬性(避免引用共享),用原型鏈繼承方法(實現復用)。ES6 的 class extends 背後,做的就是這件事的標準化版本。