1、继承的优先级
class Father {
say() {
return '我是爸爸';
}
}
class Son {
say() {
console.log('我是儿子');
}
}
let son = new Son();
son.say(); // 我是儿子
// (1)继承中,如果实例化的子类调用一个方法,首先会先看子类有没有对应的方法,如果有就执行子类对应的方法;
// 如果子类中没有查找到该方法就去父类中查找,找到调用否则报错;
2、使用super关键字直接调用父类中的方法
class Father {
say() {
return '我是爸爸';
}
}
class Son extends Father{
say() {
// 通过super关键字可以在子类中直接调用父类的普通方法
console.log(super.say())
}
}
let son = new Son();
son.say();