JavaScript 原型繼承
更新時(shí)間:2011年12月26日 23:09:09 作者:
JavaScript 原型繼承,學(xué)習(xí)js面向?qū)ο蟮呐笥芽梢钥纯础?/div>
Object.prototype
JavaScript是基于原型繼承的,任何對(duì)象都有一個(gè)prototype屬性。Object.prototype是所有對(duì)象的根,并且不可改變。
Object.prototype=null;
alert(Object.prototype);//[object Object]
Object與Object.prototype
Object繼承于Object.prototype,增加一個(gè)屬性給Object.prototype上,同時(shí)也會(huì)反應(yīng)到Object上。如:
Object.prototype.nameStr="Object Prototype";
Object.prototype.getName=function(){return this.nameStr};
alert(Object.getName());//Object Prototype
Function.prototype與Object.prototype
由于Object.prototype是萬(wàn)物之根,所以Function.prototype也同時(shí)會(huì)繼承Object.prototype的所有屬性。如:
Object.prototype.nameStr="Object Prototype";
Object.prototype.getName=function(){return this.nameStr};
alert(Function.prototype.getName());//Object Prototype
Object/Function/String/Number/Boolean/Array與Date
Object/Function/String/Number/Boolean/Array與Date都是函數(shù),函數(shù)又繼承于Function.prototype, 所以更改Function.prototype一樣會(huì)影響到Object/Function/String/Number/Boolean/Array與Date。如:
Function.prototype.initType='Function Type';
Function.prototype.getType=function(){return this.initType};
//alert(Object.getType());//Function Type
//alert(Date.getType());//Function Type
//alert(Number.getType());//Function Type
//alert(String.getType());//Function Type
//alert(Boolean.getType());//Function Type
alert(Array.getType());//Function Type
同樣Function.prototype也會(huì)把所受Object.prototype的影響,傳遞給它的下一層級(jí)。如:
Object.prototype.nameStr="Object Prototype";
Object.prototype.getName=function(){return this.nameStr};
alert(Function.prototype.getName());//Object Prototype
alert(Array.getName());//Object Prototype
alert(Boolean.prototype.getName());//Object Prototype
Array/Array.prototype與Function.prototype/Object.prototype
Array是函數(shù)對(duì)象,受Function.prototype的影響,而Array.prototype不是函數(shù)對(duì)象,所不受Function.prototype的影響,但所有對(duì)象受Object.prototype的影響,所以Array.prototype也會(huì)受Object.prototype的影響。如:
Object.prototype.nameStr="Object Prototype";
Object.prototype.getName=function(){return this.nameStr};
//alert(Function.prototype.getName());//Object Prototype
//alert(Boolean.prototype.getName());//Object Prototype
Function.prototype.initFun=function(){
return 'Function.prototype.initFun';
}
alert(Array.initFun());//Function.prototype.initFun
var arr=['a','b'];
alert(arr.getName());//Object Prototype
alert(arr.initFun());//Error: arr.initFun is not a function
alert(arr.initFun);//undefined
JavaScript是基于原型繼承的,任何對(duì)象都有一個(gè)prototype屬性。Object.prototype是所有對(duì)象的根,并且不可改變。
復(fù)制代碼 代碼如下:
Object.prototype=null;
alert(Object.prototype);//[object Object]
Object與Object.prototype
Object繼承于Object.prototype,增加一個(gè)屬性給Object.prototype上,同時(shí)也會(huì)反應(yīng)到Object上。如:
復(fù)制代碼 代碼如下:
Object.prototype.nameStr="Object Prototype";
Object.prototype.getName=function(){return this.nameStr};
alert(Object.getName());//Object Prototype
Function.prototype與Object.prototype
由于Object.prototype是萬(wàn)物之根,所以Function.prototype也同時(shí)會(huì)繼承Object.prototype的所有屬性。如:
復(fù)制代碼 代碼如下:
Object.prototype.nameStr="Object Prototype";
Object.prototype.getName=function(){return this.nameStr};
alert(Function.prototype.getName());//Object Prototype
Object/Function/String/Number/Boolean/Array與Date
Object/Function/String/Number/Boolean/Array與Date都是函數(shù),函數(shù)又繼承于Function.prototype, 所以更改Function.prototype一樣會(huì)影響到Object/Function/String/Number/Boolean/Array與Date。如:
復(fù)制代碼 代碼如下:
Function.prototype.initType='Function Type';
Function.prototype.getType=function(){return this.initType};
//alert(Object.getType());//Function Type
//alert(Date.getType());//Function Type
//alert(Number.getType());//Function Type
//alert(String.getType());//Function Type
//alert(Boolean.getType());//Function Type
alert(Array.getType());//Function Type
同樣Function.prototype也會(huì)把所受Object.prototype的影響,傳遞給它的下一層級(jí)。如:
復(fù)制代碼 代碼如下:
Object.prototype.nameStr="Object Prototype";
Object.prototype.getName=function(){return this.nameStr};
alert(Function.prototype.getName());//Object Prototype
alert(Array.getName());//Object Prototype
復(fù)制代碼 代碼如下:
alert(Boolean.prototype.getName());//Object Prototype
Array/Array.prototype與Function.prototype/Object.prototype
Array是函數(shù)對(duì)象,受Function.prototype的影響,而Array.prototype不是函數(shù)對(duì)象,所不受Function.prototype的影響,但所有對(duì)象受Object.prototype的影響,所以Array.prototype也會(huì)受Object.prototype的影響。如:
復(fù)制代碼 代碼如下:
Object.prototype.nameStr="Object Prototype";
Object.prototype.getName=function(){return this.nameStr};
//alert(Function.prototype.getName());//Object Prototype
//alert(Boolean.prototype.getName());//Object Prototype
Function.prototype.initFun=function(){
return 'Function.prototype.initFun';
}
alert(Array.initFun());//Function.prototype.initFun
var arr=['a','b'];
alert(arr.getName());//Object Prototype
alert(arr.initFun());//Error: arr.initFun is not a function
alert(arr.initFun);//undefined
相關(guān)文章
DOMAssitant最新版 DOMAssistant 2.5發(fā)布
google code上的project ,值得大家學(xué)習(xí)它的類庫(kù)2007-12-12Javascript 類與靜態(tài)類的實(shí)現(xiàn)
在Javascript里,對(duì)面向?qū)ο蟛]有一個(gè)直接的實(shí)現(xiàn),對(duì)于代碼方面也是非常的靈活。2010-04-04js對(duì)象的構(gòu)造和繼承實(shí)現(xiàn)代碼
js對(duì)象的構(gòu)造和繼承實(shí)現(xiàn)代碼,學(xué)習(xí)javascript面向?qū)ο蟮呐笥芽梢詤⒖枷隆懗龈僚c復(fù)用的代碼。2010-12-12js面向?qū)ο笤O(shè)計(jì)用{}好還是function(){}好(構(gòu)造函數(shù))
js面向?qū)ο笤O(shè)計(jì)用{}好還是function(){}好,大家給予了回復(fù),感覺不錯(cuò),特分享給大家。2011-10-10[推薦]javascript 面向?qū)ο蠹夹g(shù)基礎(chǔ)教程
看了很多介紹javascript面向?qū)ο蠹夹g(shù)的文章,很暈.為什么?不是因?yàn)閷懙貌缓?而是因?yàn)樘願(yuàn)W. javascript中的對(duì)象還沒解釋清楚怎么回事,一上來(lái)就直奔主題,類/繼承/原型/私有變量....2009-03-03javascript 面向?qū)ο缶幊? function是方法(函數(shù))
在進(jìn)行編程時(shí),必免不了要碰到復(fù)雜的功能。初學(xué)者最怕復(fù)雜的功能,因?yàn)椴荒軌蚝芎玫倪M(jìn)行功能邊界劃分,只能一大串if、循環(huán)加case堆疊在一起,結(jié)果出來(lái)的程序自己看著暈,別人看著更暈。2009-09-09