《JavaScript進階程式設計》閱讀筆記(十四):繼承機制的實現

來源:互聯網
上載者:User
繼承

  繼承是物件導向語言的必備特徵,即一個類能夠重用另一個類的方法和屬性。在JavaScript中繼承方式的實現方式主要有以下五種:對象冒充、call()、apply()、原型鏈、混合方式。

  下面分別介紹。

對象冒充

  原理:建構函式使用this關鍵字給所有屬性和方法賦值。因為建構函式只是一個函數,所以可以使ClassA的建構函式成為ClassB的方法,然後調用它。ClassB就會收到ClassA的建構函式中定義的屬性和方法。

  樣本:

function ClassA(sColor){
this.color=sColor;
this.sayColor=function(){
alert(this.color);
}
}

function ClassB(sColor,sName){
this.newMethod=ClassA;
this.newMethod(sColor);
delete this.newMethod;

this.name=sName;
this.sayName=function(){
alert(this.name);
}
}

  調用:

var objb=new ClassB("blue","Test");
objb.sayColor();// blue
objb.sayName(); // Test

  注意:這裡要刪除對ClassA的引用,否則在後面定義新的方法和屬性會覆蓋超類的相關屬性和方法。用這種方式可以實現多重繼承。

call()方法

  由於對象冒充方法的流行,在ECMAScript的第三版對Function對象加入了兩個新方法 call()和apply()方法來實現相似功能。

  call()方法的第一個參數用作this的對象,其他參數都直接傳遞給函數自身。樣本:

function sayColor(sPrefix,sSuffix){
alert(sPrefix+this.color+sSuffix);
}

var obj=new Object();
obj.color="red";

//output The color is red, a very nice color indeed.
sayColor.call(obj,"The color is ",", a very nice color indeed.");

  使用此方法來實現繼承,只需要將前三行的賦值、調用、刪除代碼替換即可:

function ClassB(sColor,sName){
//this.newMethod=ClassA;
//this.newMethod(sColor);
//delete this.newMethod;
ClassA.call(this,sColor);

this.name=sName;
this.sayName=function(){
alert(this.name);
}
}
apply()方法

  apply()方法跟call()方法類似,不同的是第二個參數,在apply()方法中傳遞的是一個數組。

function sayColor(sPrefix,sSuffix){
alert(sPrefix+this.color+sSuffix);
}

var obj=new Object();
obj.color="red";

//output The color is red, a very nice color indeed.
sayColor.apply(obj,new Array("The color is ",", a very nice color indeed."));

  使用此方法來實現繼承,只需要將前三行的賦值、調用、刪除代碼替換即可:

function ClassB(sColor,sName){
//this.newMethod=ClassA;
//this.newMethod(sColor);
//delete this.newMethod;
ClassA.apply(this,new Array(sColor));

this.name=sName;
this.sayName=function(){
alert(this.name);
}
}

  跟call()有一點不同的是,如果超類中的參數順序與子類中的參數順序完全一致,第二個參數可以用arguments。

原型鏈

  繼承這種形式在ECMAScript中原本是用於原型鏈的。Prototype對象的任何屬性和方法都被傳遞給那個類的所有執行個體。原型鏈利用這種功能實現繼承機制。

  用原型鏈實現繼承樣本:

function ClassA(){
}

ClassA.prototype.color="red";
ClassA.prototype.sayColor=function(){
alert(this.color);
};

function ClassB(){
}

ClassB.prototype=new ClassA();

  注意:調用ClassA的建構函式時,沒有給它傳遞參數。這在原型鏈中是標準的做法,要確保建構函式沒有任何參數。

混合方式

  這種方式混合了對象冒充和原型鏈方式。樣本:

function ClassA(sColor){
this.color=sColor;
}
ClassA.prototype.sayColor=function(){
alert(this.color);
}

function ClassB(sColor,sName){
ClassA.call(this,sColor);
this.name=sName;
}

ClassB.prototype=new ClassA();
ClassB.prototype.sayName=function(){
alert(this.name);
}

  調用樣本:

var objb=new ClassB("red","test");
objb.sayColor();// output red
objb.sayName();// output test
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.