JavaScript中創(chuàng)建對象和繼承示例解讀
更新時(shí)間:2014年02月12日 15:26:12 作者:
這篇文章主要介紹了JavaScript中怎樣創(chuàng)建對象和繼承,需要的朋友可以參考下
對象創(chuàng)建:
當(dāng)一個(gè)函數(shù)對象被創(chuàng)建時(shí)候,F(xiàn)unction構(gòu)造器產(chǎn)生的函數(shù)對象會運(yùn)行類似這樣的代碼:
this.prototype={constructor:this};
假設(shè)函數(shù)F
F用new方式構(gòu)造對象時(shí),對象的constructor被設(shè)置成這個(gè)F.prototype.constructor
如果函數(shù)在創(chuàng)建對象前修改了函數(shù)的prototype,會影響創(chuàng)建出來對象的construtor屬性
如:
function F(){};
F.prototype={constructor:'1111'};
var o=new F();//o.constructor===‘1111' true
繼承原理:
JavaScript中的繼承是使用原型鏈的機(jī)制,每個(gè)函數(shù)的實(shí)例都共享構(gòu)造函數(shù)prototype屬性中定義的數(shù)據(jù),要使一個(gè)類繼承另一個(gè),需要把父函數(shù)實(shí)例賦值到子函數(shù)的prototype屬性。并且在每次new實(shí)例對象時(shí),對象的私有屬性__proto__會被自動連接到構(gòu)造函數(shù)的prototype。
instanceof就是查找實(shí)例對象的私有prototype屬性鏈來確定是否是指定對象的實(shí)例
具體實(shí)例:
//instanceof實(shí)現(xiàn)
function Myinstanceof(obj,type)
{
var proto=obj.__proto__;
while(proto)
{
if(proto ===type.prototype)break;
proto=proto.__proto__;
}
return proto!=null;
}
function View(){}
function TreeView(){}
TreeView.prototype=new View();//TreeView.prototype.__proto__=TreeView.prototype 自動完成
TreeView.prototype.constructor=TreeView;//修正constructor
var view=new TreeView();//view.__proto__=TreeView.prototype 自動完成
alert(view instanceof View); //true 查找到view.__proto__.__proto__時(shí)找到
alert(view instanceof TreeView); //true 查找到view.__proto__時(shí)找到
alert(Myinstanceof(view,View)); //true
alert(Myinstanceof(view,TreeView)); //true
當(dāng)一個(gè)函數(shù)對象被創(chuàng)建時(shí)候,F(xiàn)unction構(gòu)造器產(chǎn)生的函數(shù)對象會運(yùn)行類似這樣的代碼:
復(fù)制代碼 代碼如下:
this.prototype={constructor:this};
假設(shè)函數(shù)F
F用new方式構(gòu)造對象時(shí),對象的constructor被設(shè)置成這個(gè)F.prototype.constructor
如果函數(shù)在創(chuàng)建對象前修改了函數(shù)的prototype,會影響創(chuàng)建出來對象的construtor屬性
如:
復(fù)制代碼 代碼如下:
function F(){};
F.prototype={constructor:'1111'};
var o=new F();//o.constructor===‘1111' true
繼承原理:
JavaScript中的繼承是使用原型鏈的機(jī)制,每個(gè)函數(shù)的實(shí)例都共享構(gòu)造函數(shù)prototype屬性中定義的數(shù)據(jù),要使一個(gè)類繼承另一個(gè),需要把父函數(shù)實(shí)例賦值到子函數(shù)的prototype屬性。并且在每次new實(shí)例對象時(shí),對象的私有屬性__proto__會被自動連接到構(gòu)造函數(shù)的prototype。
instanceof就是查找實(shí)例對象的私有prototype屬性鏈來確定是否是指定對象的實(shí)例
具體實(shí)例:
復(fù)制代碼 代碼如下:
//instanceof實(shí)現(xiàn)
function Myinstanceof(obj,type)
{
var proto=obj.__proto__;
while(proto)
{
if(proto ===type.prototype)break;
proto=proto.__proto__;
}
return proto!=null;
}
function View(){}
function TreeView(){}
TreeView.prototype=new View();//TreeView.prototype.__proto__=TreeView.prototype 自動完成
TreeView.prototype.constructor=TreeView;//修正constructor
var view=new TreeView();//view.__proto__=TreeView.prototype 自動完成
alert(view instanceof View); //true 查找到view.__proto__.__proto__時(shí)找到
alert(view instanceof TreeView); //true 查找到view.__proto__時(shí)找到
alert(Myinstanceof(view,View)); //true
alert(Myinstanceof(view,TreeView)); //true
相關(guān)文章
微信小程序點(diǎn)擊滾動到指定位置的實(shí)現(xiàn)
這篇文章主要介紹了微信小程序點(diǎn)擊滾動到指定位置的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
自己寫的Javascript計(jì)算時(shí)間差函數(shù)
Javascript計(jì)算時(shí)間差函數(shù),獲得時(shí)間差,時(shí)間格式為 年-月-日 小時(shí):分鐘:秒 或者 年/月/日 小時(shí):分鐘:秒。2013-10-10
JavaScript將數(shù)據(jù)轉(zhuǎn)換成整數(shù)的方法
這篇文章主要介紹了JavaScript將數(shù)據(jù)轉(zhuǎn)換成整數(shù)的方法,有需要的朋友可以參考一下2014-01-01
uniapp動態(tài)修改元素節(jié)點(diǎn)樣式詳解
這篇文章主要介紹了uni-app動如何態(tài)修改元素節(jié)點(diǎn)樣式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-08-08
echarts報(bào)錯(cuò):Error?in?mounted?hook的解決方法
最近又遇到了寫echarts的時(shí)候常遇到的一個(gè)錯(cuò)誤,這篇文章主要給大家介紹了關(guān)于echarts報(bào)錯(cuò):Error?in?mounted?hook:?“TypeError:?Cannot?read?properties?of?undefined?(reading?‘init‘)“的解決方法,需要的朋友可以參考下2022-07-07
js實(shí)現(xiàn)WebSocket 連接的示例代碼
本文主要介紹了js實(shí)現(xiàn)WebSocket 連接的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05

