JavaScript繼承方式實例
更新時間:2010年10月29日 21:30:08 作者:
JavaScript繼承方式實例,需要的朋友可以參考下。
復(fù)制代碼 代碼如下:
function parent(){
this.x=10;
}
function child(){
var parentObj=new parent();
for(var p in parentObj)this[p]=parentObj[p];
}
var childObj=new child();
alert(childObj.x);
復(fù)制代碼 代碼如下:
function parent(){
this.x=10;
}
function child(){
this.parent=parent;
this.parent();
delete this.parent;
}
var childObj=new child();
alert(childObj.x);
復(fù)制代碼 代碼如下:
function parent(){
this.x=10;
}
function child(){
parent.call(this);
}
var childObj=new child();
alert(childObj.x);
原型抄寫
復(fù)制代碼 代碼如下:
function parent(){
}
parent.prototype.x=1;
function child(){
}
for(var p in parent.prototype)child.prototype[p]=parent.prototype[p];
child.prototype.y=2;
var childObj=new child();
alert(childObj.x);
復(fù)制代碼 代碼如下:
function parent(string){
var child=new Function("this.x=10;"+string);
return child;
}
var child=new parent("this.y=20;");
var childObj=new child();
alert(childObj.y);
復(fù)制代碼 代碼如下:
function parent(){
this.x=10;
}
function child(){
}
child.prototype=new parent();
var childObj=new child();
alert(childObj.x);
復(fù)制代碼 代碼如下:
function parent(){
this.x=10;
}
function child(){
var ret=new parent();
ret.y=20;
return ret;
}
var childObj=new child();
alert(childObj.x);
相關(guān)文章
js仿微博實現(xiàn)統(tǒng)計字符和本地存儲功能
這篇文章主要介紹了js仿微博實現(xiàn)統(tǒng)計字符和本地存儲功能的相關(guān)資料,需要的朋友可以參考下2015-12-12
微信小程序?qū)崿F(xiàn)客服功能(客服消息)的全過程
在最近做的微信小程序中需要實現(xiàn)一個自帶的客服功能,下面這篇文章主要給大家介紹了關(guān)于微信小程序?qū)崿F(xiàn)客服功能(客服消息)的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-12-12
基于JavaScript實現(xiàn)復(fù)選框的全選和取消全選
這篇文章主要為大家詳細介紹了基于JavaScript實現(xiàn)復(fù)選框的全選和取消全選,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02
CSS3+JavaScript實現(xiàn)翻頁幻燈片效果
這篇文章主要介紹了CSS3+JavaScript實現(xiàn)翻頁幻燈片效果,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-06-06
ES6 新增的創(chuàng)建數(shù)組的方法(小結(jié))
這篇文章主要介紹了ES6 新增的創(chuàng)建數(shù)組的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
JavaScript中的await函數(shù)使用小結(jié)
async 函數(shù)是 AsyncFunction 構(gòu)造函數(shù)的實例,并且其中允許使用 await 關(guān)鍵字,async 和 await 關(guān)鍵字讓我們可以用一種更簡潔的方式寫出基于 Promise 的異步行為,而無需刻意地鏈?zhǔn)秸{(diào)用 promise,這篇文章主要介紹了JavaScript中的await,需要的朋友可以參考下2024-01-01

