前端修鍊-javascript關鍵字之prototype

來源:互聯網
上載者:User

標籤:bsp   type   ext   自身   end   建立   instance   this   obj   

   通常來說,javascript中的對象就是一個指向prototype的指標和一個自身的屬性列表。只有構造器(Object,Function,Array,Date,String)才具有且會有一個prototype屬性。原型鏈繼承就是建立一個新的指標,指向構造器的prototype屬性。

 prototype屬性列表會預設有一個constructor(建構函式)屬性,這個屬性是一個指向prototype屬性所在指標,也就是當前對象自己。

    

 1 function baseClass() 2 { 3     this.showMsg = function() 4     { 5         alert("baseClass::showMsg");    6     } 7     8     this.baseShowMsg = function() 9     {10         alert("baseClass::baseShowMsg");11     }12 }13 baseClass.showMsg = function()14 {15     alert("baseClass::showMsg static");16 }17 18 function extendClass()19 {20     this.showMsg =function ()21     {22         alert("extendClass::showMsg");23     }24 }25 extendClass.showMsg = function()26 {27     alert("extendClass::showMsg static")28 }29 30 extendClass.prototype = new baseClass();31 var instance = new extendClass();32 33 instance.showMsg(); //顯示extendClass::showMsg34 instance.baseShowMsg(); //顯示baseClass::baseShowMsg35 instance.showMsg(); //顯示extendClass::showMsg36 37 baseClass.showMsg.call(instance);//顯示baseClass::showMsg static38 39 var baseinstance = new baseClass();40 baseinstance.showMsg.call(instance);//顯示baseClass::showMsg

 

前端修鍊-javascript關鍵字之prototype

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.