说明
与 Java 类似,通过 extends
继承对于代码可重用性很有用:在创建子类时,继承父类,就可以重用父类的属性和方法
class Car {
constructor(brand) {
this.carname = brand;
}
present() {
return 'I have a ' + this.carname;
}
}
class Model extends Car {
constructor(brand, mod) {
super(brand);
this.model = mod;
}
show() {
return this.present() + ', it is a ' + this.model;
}
}
let myCar = new Model("Ford", "Mustang");
document.getElementById("demo").innerHTML = myCar.show();
super()
通过 super() 方法调用父类的构造方法
通过在 constructor() 方法中调用 super() 方法,调用了父类的 constructor() 方法