JavaScript對象創(chuàng)建及繼承原理實(shí)例解剖
更新時(shí)間:2013年02月28日 10:50:20 作者:
本文將用實(shí)例講解一下JavaScript對象創(chuàng)建及繼承原理:JavaScript中的繼承是使用原型鏈的機(jī)制,對象創(chuàng)建使用Function構(gòu)造器,感興趣的朋友可以詳細(xì)了解下本文,或許可以幫助到你
對象創(chuàng)建:
當(dāng)一個(gè)函數(shù)對象被創(chuàng)建時(shí)候,F(xiàn)unction構(gòu)造器產(chǎn)生的函數(shù)對象會(huì)運(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,會(huì)影響創(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__會(huì)被自動(dòng)連接到構(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 自動(dòng)完成
TreeView.prototype.constructor=TreeView;//修正constructor
var view=new TreeView();//view.__proto__=TreeView.prototype 自動(dòng)完成
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
上面自定義的Myinstanceof就是自己實(shí)現(xiàn)的一個(gè)instanceof功能的函數(shù),由于IE內(nèi)核實(shí)例存儲(chǔ)prototype不是__proto__,所以Myinstanceof會(huì)無法通過,其他瀏覽器上應(yīng)該都沒有問題
當(dāng)一個(gè)函數(shù)對象被創(chuàng)建時(shí)候,F(xiàn)unction構(gòu)造器產(chǎn)生的函數(shù)對象會(huì)運(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,會(huì)影響創(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__會(huì)被自動(dòng)連接到構(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 自動(dòng)完成
TreeView.prototype.constructor=TreeView;//修正constructor
var view=new TreeView();//view.__proto__=TreeView.prototype 自動(dòng)完成
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
上面自定義的Myinstanceof就是自己實(shí)現(xiàn)的一個(gè)instanceof功能的函數(shù),由于IE內(nèi)核實(shí)例存儲(chǔ)prototype不是__proto__,所以Myinstanceof會(huì)無法通過,其他瀏覽器上應(yīng)該都沒有問題
相關(guān)文章
Javascript基礎(chǔ)_簡單比較undefined和null 值
下面小編就為大家?guī)硪黄狫avascript基礎(chǔ)_簡單比較undefined和null 值。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06JavaScript學(xué)習(xí)筆記之Function對象
這篇文章主要介紹了JavaScript學(xué)習(xí)筆記之Function對象的相關(guān)資料,需要的朋友可以參考下2015-01-01JavaScript函數(shù)中上下文有哪些規(guī)則
上下文是從英文context翻譯過來,指的是一種環(huán)境。在軟件工程中,上下文是一種屬性的有序序列,它們?yōu)轳v留在環(huán)境內(nèi)的對象定義環(huán)境。在對象的激活過程中創(chuàng)建上下文,對象被配置為要求某些自動(dòng)服務(wù)。又比如計(jì)算機(jī)技術(shù)中,相對于進(jìn)程而言,上下文就是進(jìn)程執(zhí)行時(shí)的環(huán)境2021-10-10你必須知道的JavaScript 中字符串連接的性能的一些問題
每種程序語言中都會(huì)涉及到字符竄連接,而這個(gè)小小的字符竄連接問題很可能會(huì)影響到系統(tǒng)的整體性能,本文主要探討JavaScript中字符串連接的性能問題2013-05-05javascript學(xué)習(xí)筆記(十) js對象 繼承
javascript學(xué)習(xí)筆記之js對象 繼承介紹,需要的朋友可以參考下2012-06-06JavaScript sup方法入門實(shí)例(把字符串顯示為上標(biāo))
這篇文章主要介紹了JavaScript sup方法入門實(shí)例,sup方法用于把字符串顯示為上標(biāo),需要的朋友可以參考下2014-10-10JavaScript中使用指數(shù)方法Math.exp()的簡介
這篇文章主要介紹了JavaScript中使用指數(shù)方法Math.exp(),是JS入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-06-06