javascript原型鏈繼承用法實(shí)例分析
本文實(shí)例分析了javascript原型鏈繼承的用法。分享給大家供大家參考。具體分析如下:
this.name = 'shape';
this.toString = function(){
return this.name;
}
}
function TwoDShape(){
this.name = '2D shape';
}
function Triangle(side,height){
this.name = 'Triangle';
this.side = side;
this.height = height;
this.getArea = function(){
return this.side*this.height/2;
};
}
/* inheritance */
TwoDShape.prototype = new Shape();
Triangle.prototype = new TwoDShape();
當(dāng)我們對(duì)對(duì)象的prototype屬性進(jìn)行完全重寫時(shí),有時(shí)候會(huì)對(duì)對(duì)象constructor屬性產(chǎn)生一定的負(fù)面影響。
所以,在我們完成相關(guān)的繼承關(guān)系設(shè)定后,對(duì)這些對(duì)象的const屬性進(jìn)行相應(yīng)的重置是一個(gè)非常好的習(xí)慣。如下所示:
Triangle.prototype.constructor = Triangle;
改寫:
Shape.prototype.name = 'shape';
Shape.prototype.toString = function(){
return this.name;
}
function TwoDShape(){}
TwoDShape.prototype = new Shape();
TwoDShape.prototype.constructor = TwoDShape;
TwoDShape.prototype.name = '2d shape';
function Triangle(side,height){
this.side = side;
this.height = height;
}
Triangle.prototype = new TwoDShape;
Triangle.prototype.constructor = Triangle;
Triangle.prototype.name = 'Triangle';
Triangle.prototype.getArea = function(){
return this.side*this.height/2;
}
再改寫(引用傳遞而不是值傳遞):
Shape.prototype.name = 'shape';
Shape.prototype.toString = function(){
return this.name;
}
function TwoDShape(){}
TwoDShape.prototype = Shape.prototype;
TwoDShape.prototype.constructor = TwoDShape;
TwoDShape.prototype.name = '2d shape';
function Triangle(side,height){
this.side = side;
this.height = height;
}
Triangle.prototype = TwoDShape.prototype;
Triangle.prototype.constructor = Triangle;
Triangle.prototype.name = 'Triangle';
Triangle.prototype.getArea = function(){
return this.side*this.height/2;
}
雖然提高了效率,但是這樣的方法有個(gè)副作用,因?yàn)槭且脗鬟f,而不是值傳遞,所以“父對(duì)象”中的name值受到了影響。
子對(duì)象和父對(duì)象指向的是同一個(gè)對(duì)象。所以一旦子對(duì)象對(duì)其原型進(jìn)行修改,父對(duì)象也會(huì)隨即被改變。
再再改寫(使用臨時(shí)構(gòu)造器):
Shape.prototype.name = 'shape';
Shape.prototype.toString = function(){
return this.name;
}
function TwoDShape(){}
var F = function(){}
F.prototype = Shape.prototype;
TwoDShape.prototype = new F();
TwoDShape.prototype.constructor = TwoDShape;
TwoDShape.prototype.name = '2d shape';
function Triangle(side,height){
this.side = side;
this.height = height;
}
F.prototype = TwoDShape.prototype;
Triangle.prototype = new F();
Triangle.prototype.constructor = Triangle;
Triangle.prototype.name = 'Triangle';
Triangle.prototype.getArea = function(){
return this.side*this.height/2;
}
雖然提高了效率,但是這樣的方法有個(gè)副作用,因?yàn)槭且脗鬟f,而不是值傳遞,所以“父對(duì)象”中的name值受到了影響。
子對(duì)象和父對(duì)象指向的是同一個(gè)對(duì)象。所以一旦子對(duì)象對(duì)齊原型進(jìn)行修改,父對(duì)象也會(huì)隨即被改變。
希望本文所述對(duì)大家的javascript程序設(shè)計(jì)有所幫助。
相關(guān)文章
js數(shù)組如何添加json數(shù)據(jù)及js數(shù)組與json的區(qū)別
在JSON中,有兩種數(shù)據(jù)結(jié)構(gòu):對(duì)象和數(shù)組。本篇文章給大家介紹js數(shù)組如何添加json數(shù)據(jù)以及js數(shù)組和json的區(qū)別,涉及到j(luò)s json數(shù)組添加相關(guān)知識(shí),感興趣的朋友可以參考下本篇文章2015-10-10
JS中利用FileReader實(shí)現(xiàn)上傳圖片前本地預(yù)覽功能
FileReader 對(duì)象允許Web應(yīng)用程序異步讀取存儲(chǔ)在用戶計(jì)算機(jī)上的文件(或原始數(shù)據(jù)緩沖區(qū))的內(nèi)容,使用 File 或 Blob 對(duì)象指定要讀取的文件或數(shù)據(jù)。下面通過本文給大家介紹JS中利用FileReader實(shí)現(xiàn)上傳圖片前本地預(yù)覽功能,需要的朋友參考下2018-03-03
JavaScript中如何讓?x?==?1?&&?x?==?2?&&?x?==?3?等式成立
這篇文章主要介紹了JavaScript中如何讓x==1&&x==2&&x==3等式成立,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-07-07
JavaScript中字符串與Unicode編碼互相轉(zhuǎn)換的實(shí)現(xiàn)方法
這篇文章主要介紹了JavaScript中字符串與Unicode編碼互相轉(zhuǎn)換的實(shí)現(xiàn)方法涉及JavaScript編碼、數(shù)據(jù)類型等的轉(zhuǎn)換技巧,需要的朋友可以參考下2015-12-12
JS平滑無縫滾動(dòng)效果的實(shí)現(xiàn)代碼
下面小編就為大家?guī)硪黄狫S平滑無縫滾動(dòng)效果的實(shí)現(xiàn)代碼。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考2016-05-05
layui動(dòng)態(tài)渲染生成select的option值方法
今天小編就為大家分享一篇layui動(dòng)態(tài)渲染生成select的option值方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-09-09

