JavaScript高級程序設計 閱讀筆記(十四) js繼承機制的實現(xiàn)
更新時間:2012年08月14日 16:19:44 作者:
繼承是面向對象語言的必備特征,即一個類能夠重用另一個類的方法和屬性。在JavaScript中繼承方式的實現(xiàn)方式主要有以下五種:對象冒充、call()、apply()、原型鏈、混合方式
繼承
繼承是面向對象語言的必備特征,即一個類能夠重用另一個類的方法和屬性。在JavaScript中繼承方式的實現(xiàn)方式主要有以下五種:對象冒充、call()、apply()、原型鏈、混合方式。
下面分別介紹。
對象冒充
原理:構造函數(shù)使用this關鍵字給所有屬性和方法賦值。因為構造函數(shù)只是一個函數(shù),所以可以使ClassA的構造函數(shù)成為ClassB的方法,然后調用它。ClassB就會收到ClassA的構造函數(shù)中定義的屬性和方法。
示例:
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();//
blueobjb.sayName(); // Test
注意:這里要刪除對ClassA的引用,否則在后面定義新的方法和屬性會覆蓋超類的相關屬性和方法。用這種方式可以實現(xiàn)多重繼承。
call()方法
由于對象冒充方法的流行,在ECMAScript的第三版對Function對象加入了兩個新方法 call()和apply()方法來實現(xiàn)相似功能。
call()方法的第一個參數(shù)用作this的對象,其他參數(shù)都直接傳遞給函數(shù)自身。示例:
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.");
使用此方法來實現(xiàn)繼承,只需要將前三行的賦值、調用、刪除代碼替換即可:
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()方法類似,不同的是第二個參數(shù),在apply()方法中傳遞的是一個數(shù)組。
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."));
使用此方法來實現(xiàn)繼承,只需要將前三行的賦值、調用、刪除代碼替換即可:
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()有一點不同的是,如果超類中的參數(shù)順序與子類中的參數(shù)順序完全一致,第二個參數(shù)可以用arguments。
原型鏈
繼承這種形式在ECMAScript中原本是用于原型鏈的。Prototype對象的任何屬性和方法都被傳遞給那個類的所有實例。原型鏈利用這種功能實現(xiàn)繼承機制。
用原型鏈實現(xiàn)繼承示例:
function ClassA(){
}
ClassA.prototype.color="red";
ClassA.prototype.sayColor=function(){
alert(this.color);
};
function ClassB(){
}
ClassB.prototype=new ClassA();
注意:調用ClassA的構造函數(shù)時,沒有給它傳遞參數(shù)。這在原型鏈中是標準的做法,要確保構造函數(shù)沒有任何參數(shù)。
混合方式
這種方式混合了對象冒充和原型鏈方式。示例:
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
作者:Artwl
繼承是面向對象語言的必備特征,即一個類能夠重用另一個類的方法和屬性。在JavaScript中繼承方式的實現(xiàn)方式主要有以下五種:對象冒充、call()、apply()、原型鏈、混合方式。
下面分別介紹。
對象冒充
原理:構造函數(shù)使用this關鍵字給所有屬性和方法賦值。因為構造函數(shù)只是一個函數(shù),所以可以使ClassA的構造函數(shù)成為ClassB的方法,然后調用它。ClassB就會收到ClassA的構造函數(shù)中定義的屬性和方法。
示例:
復制代碼 代碼如下:
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();//
blueobjb.sayName(); // Test
注意:這里要刪除對ClassA的引用,否則在后面定義新的方法和屬性會覆蓋超類的相關屬性和方法。用這種方式可以實現(xiàn)多重繼承。
call()方法
由于對象冒充方法的流行,在ECMAScript的第三版對Function對象加入了兩個新方法 call()和apply()方法來實現(xiàn)相似功能。
call()方法的第一個參數(shù)用作this的對象,其他參數(shù)都直接傳遞給函數(shù)自身。示例:
復制代碼 代碼如下:
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.");
使用此方法來實現(xiàn)繼承,只需要將前三行的賦值、調用、刪除代碼替換即可:
復制代碼 代碼如下:
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()方法類似,不同的是第二個參數(shù),在apply()方法中傳遞的是一個數(shù)組。
復制代碼 代碼如下:
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."));
使用此方法來實現(xiàn)繼承,只需要將前三行的賦值、調用、刪除代碼替換即可:
復制代碼 代碼如下:
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()有一點不同的是,如果超類中的參數(shù)順序與子類中的參數(shù)順序完全一致,第二個參數(shù)可以用arguments。
原型鏈
繼承這種形式在ECMAScript中原本是用于原型鏈的。Prototype對象的任何屬性和方法都被傳遞給那個類的所有實例。原型鏈利用這種功能實現(xiàn)繼承機制。
用原型鏈實現(xiàn)繼承示例:
復制代碼 代碼如下:
function ClassA(){
}
ClassA.prototype.color="red";
ClassA.prototype.sayColor=function(){
alert(this.color);
};
function ClassB(){
}
ClassB.prototype=new ClassA();
注意:調用ClassA的構造函數(shù)時,沒有給它傳遞參數(shù)。這在原型鏈中是標準的做法,要確保構造函數(shù)沒有任何參數(shù)。
混合方式
這種方式混合了對象冒充和原型鏈方式。示例:
復制代碼 代碼如下:
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
作者:Artwl
您可能感興趣的文章:
- Javascript 繼承機制的實現(xiàn)
- Javascript 繼承機制實例
- javascript類繼承機制的原理分析
- JavaScript 繼承機制的實現(xiàn)(待續(xù))
- Javascript繼承機制的設計思想分享
- 由JavaScript中call()方法引發(fā)的對面向對象繼承機制call的思考
- 基于JavaScript實現(xiàn)繼承機制之構造函數(shù)+原型鏈混合方式的使用詳解
- 基于JavaScript實現(xiàn)繼承機制之原型鏈(prototype chaining)的詳解
- 基于JavaScript實現(xiàn)繼承機制之調用call()與apply()的方法詳解
- 基于JavaScript實現(xiàn)繼承機制之構造函數(shù)方法對象冒充的使用詳解
- javascript繼承機制實例詳解
- JavaScript不使用prototype和new實現(xiàn)繼承機制
相關文章
layer.open組件獲取彈出層頁面變量、函數(shù)的實例
今天小編就為大家分享一篇layer.open組件獲取彈出層頁面變量、函數(shù)的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09javascript實現(xiàn)驗證IP地址等相關信息代碼
本文給大家分享的是一組判斷IP地址是否合法,判斷子網掩碼是否合法,驗證輸入的網關地址是否合法的javascript代碼,十分的簡單實用,有需要的小伙伴可以參考下。2015-05-05js 關鍵詞高亮(根據(jù)ID/tag高亮關鍵字)案例介紹
關鍵詞高亮在開發(fā)中會帶來很多的方便,關鍵詞高亮包括:根據(jù)ID高亮關鍵字/根據(jù)Tag名高亮關鍵字等等,感興趣的朋友可以了解下,希望本文對你有所幫助2013-01-01javascript addLoadEvent函數(shù)說明
網頁加載完整后會觸發(fā)一個onload事件,默認地一個事件只能和一個函數(shù)綁定。2010-01-01js超時調用setTimeout和間歇調用setInterval實例分析
這篇文章主要介紹了js超時調用setTimeout和間歇調用setInterval,以實例形式對比分析了setTimeout與setInterval的具體使用技巧,非常具有實用價值,需要的朋友可以參考下2015-01-01javascript簡單實現(xiàn)等比例縮小圖片的方法
這篇文章主要介紹了javascript簡單實現(xiàn)等比例縮小圖片的方法,涉及javascript針對頁面元素屬性的讀取、運算及設置相關技巧,需要的朋友可以參考下2016-07-07簡單實現(xiàn)節(jié)流函數(shù)和防抖函數(shù)過程解析
這篇文章主要介紹了簡單實現(xiàn)節(jié)流函數(shù)和防抖函數(shù)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-10-10