JavaScript 原型繼承
更新時間:2011年12月26日 23:09:09 作者:
JavaScript 原型繼承,學習js面向?qū)ο蟮呐笥芽梢钥纯础?/div>
Object.prototype
JavaScript是基于原型繼承的,任何對象都有一個prototype屬性。Object.prototype是所有對象的根,并且不可改變。
Object.prototype=null;
alert(Object.prototype);//[object Object]
Object與Object.prototype
Object繼承于Object.prototype,增加一個屬性給Object.prototype上,同時也會反應(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是萬物之根,所以Function.prototype也同時會繼承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一樣會影響到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也會把所受Object.prototype的影響,傳遞給它的下一層級。如:
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ù)對象,受Function.prototype的影響,而Array.prototype不是函數(shù)對象,所不受Function.prototype的影響,但所有對象受Object.prototype的影響,所以Array.prototype也會受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是基于原型繼承的,任何對象都有一個prototype屬性。Object.prototype是所有對象的根,并且不可改變。
復(fù)制代碼 代碼如下:
Object.prototype=null;
alert(Object.prototype);//[object Object]
Object與Object.prototype
Object繼承于Object.prototype,增加一個屬性給Object.prototype上,同時也會反應(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是萬物之根,所以Function.prototype也同時會繼承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一樣會影響到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也會把所受Object.prototype的影響,傳遞給它的下一層級。如:
復(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ù)對象,受Function.prototype的影響,而Array.prototype不是函數(shù)對象,所不受Function.prototype的影響,但所有對象受Object.prototype的影響,所以Array.prototype也會受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)文章
詳解new function(){}和function(){}() 區(qū)別分析
只要 new 表達式之后的 constructor 返回(return)一個引用對象(數(shù)組,對象,函數(shù)等),都將覆蓋new創(chuàng)建的匿名對象,如果返回(return)一個原始類型(無 return 時其實為 return 原始類型 undefined),那么就返回 new 創(chuàng)建的匿名對象。2008-03-03
javascript 面向?qū)ο蟮慕?jīng)典實例代碼
這里的面向?qū)ο笾饕鞘褂胮rototype屬性,大家可以參考下。2009-12-12
js 面向?qū)ο蟮募夹g(shù)創(chuàng)建高級 Web 應(yīng)用程序
在 C++ 或 C# 中,在談?wù)搶ο髸r,是指類或結(jié)構(gòu)的實例。對象有不同的屬性和方法,具體取決于將它們實例化的模板(即類)。2010-02-02
學習面向?qū)ο笾嫦驅(qū)ο蟮幕靖拍?對象和其他基本要素
學習面向?qū)ο笾嫦驅(qū)ο蟮幕靖拍?對象和其他基本要素2010-11-11
JS類定義原型方法的兩種實現(xiàn)的區(qū)別評論很多
JS類定義原型方法的兩種實現(xiàn)的區(qū)別評論很多...2007-09-09
面向?qū)ο蟮腏avascript之二(接口實現(xiàn)介紹)
接口是面向?qū)ο驤avascript工具箱中最有用的特性之一。我們都知道GOF在設(shè)計模式中說到:面向接口編程,而非面向?qū)崿F(xiàn)編程2012-01-01

