通过 extends 实现类的继承。
class Parent {
constructor(name,age){
this.name = name;
this.age =age;
}
breath(){
console.log("呼呀,吸呀!")
}
}
class Son extends Parent{
constructor(name,age,weight){
super(name,age);
// 调用父亲的构造函数
this.weight = weight;
}
work(){
console.log("搬砖再搬砖");
}
}
实例化
var s = new Son("小木", 18,20)
s.breath();
// 呼呀,吸呀!
s.work();
// 搬砖再搬砖