解读typescript中 super关键字的用法

解读typescript中 super关键字的用法typescript

传统的js,使用prototype实现父、子类继承.
若是父、子类有同名的方法,子类去调用父类的同名方法须要用 “父类.prototype.method.call(this)”.
可是在typescript中,提供了一个关键字super,指向父类.
super.method() 这样就能够达到调用父类同名的方法.this

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();
相关文章
相关标签/搜索