最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript 原型繼承

 更新時間:2011年12月26日 23:09:09   作者:  
JavaScript 原型繼承,學習js面向?qū)ο蟮呐笥芽梢钥纯础?/div>
Object.prototype
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)文章

最新評論

南溪县| 琼结县| 集安市| 贞丰县| 灵璧县| 遂昌县| 巨野县| 九江县| 甘南县| 平泉县| 日喀则市| 武强县| 鸡西市| 积石山| 普兰县| 珠海市| 固始县| 阜阳市| 永宁县| 万山特区| 乐平市| 石景山区| 贵港市| 榆社县| 黄大仙区| 深圳市| 青铜峡市| 宜州市| 大化| 安远县| 丘北县| 怀化市| 绵竹市| 廊坊市| 平原县| 武城县| 兰坪| 盖州市| 桦南县| 南和县| 苍南县|