代码点读机 / TypeScript / super

super TypeScript · 类型关键字

在子类里指代父类:super.方法() 调父类的同名方法,super(参数) 调父类的构造函数。

怎么用

class 子类 extends 父类 {
  constructor() {
    super();
  }
}

小例子

class 动物 {
  move() { return "动"; }
}
class 狗 extends 动物 {
  move() { return super.move() + "得飞快"; }
}
console.log(new 狗().move());

运行结果

动得飞快

提醒:子类写了 constructor 就必须先调 super() 才能用 this——常见报错点

官方文档:https://www.typescriptlang.org/docs/handbook/2/classes.html#super-calls

在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← TypeScript全部词条