解讀typescript中 super關鍵字的用法


解讀typescript中 super關鍵字的用法

傳統的js,使用prototype實現父、子類繼承.
如果父、子類有同名的方法,子類去調用父類的同名方法需要用 “父類.prototype.method.call(this)”.
但是在typescript中,提供了一個關鍵字super,指向父類.
super.method() 這樣就可以達到調用父類同名的方法.

class Animal {
      constructor() {
             console.log('animal')
      }
      get() {
             console.log("吃飯")
      }     
}  

class Monkey extends Animal {
      constructor() {
              console.log("child---monkey")
              super()
      }
      get() {
             console.log("不吃飯")
      }
      init() {
              super.get()
      }
}  

var animal = new Monkey();
animal.init();

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM