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

