深入解析JavaScript 原型繼承
JavaScript/">JavaScript 原型繼承,學習js面向物件的朋友可以看看。十分的全面細緻,具有一定的參考價值,對此有需要的朋友可以參考學習下。如有不足之處,歡迎批評指正。

Object.prototype
JavaScript是基於原型繼承的,任何物件都有一個prototype屬性。Object.prototype是所有物件的根,並且不可改變。
Object.prototype=null; alert(Object.prototype);//[object Object]
Object與Object.prototype
Object繼承於Object.prototype,增加一個屬性給Object.prototype上,同時也會反應到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都是函式,函式又繼承於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 //歡迎加入前端全棧開發交流圈一起學習交流:864305860 alert(Boolean.prototype.getName());//Object Prototype //歡迎加入前端全棧開發交流圈一起學習交流:864305860 Array/Array.prototype與Function.prototype/Object.prototype
Array是函式物件,受Function.prototype的影響,而Array.prototype不是函式物件,所不受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'; } //歡迎加入前端全棧開發交流圈一起學習交流:864305860 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 //歡迎加入前端全棧開發交流圈一起學習交流:864305860
結語
感謝您的觀看,如有不足之處,歡迎批評指正。
本次給大家推薦一個免費的學習群,裡面概括移動應用網站開發,css,html,webpack,vue node angular以及面試資源等。
對web開發技術感興趣的同學,歡迎加入Q群:864305860,不管你是小白還是大牛我都歡迎,還有大牛整理的一套高效率學習路線和教程與您免費分享,同時每天更新視訊資料。
最後,祝大家早日學有所成,拿到滿意offer,快速升職加薪,走上人生巔峰。