class Human{
static str = 'aaa';
sayStr = ()=>{
throw new Error('need to implement');
}
}
class Student extends Human{
constructor(){
super();
}
sayStr(){
console.log(Student.str)
}
}
const leo = new Student();
console.log(Student.str);
leo.sayStr();
答案
aaa
need to implement
解析:
1. 在 ES6 中类的继承是可以继承静态属性的
2. 在 class 里,用 = 声明的变量属于 Field declarations 的语法,leo 的 sayStr 被挂载到了实例属性上,读取优先于原型链
本文探讨了ES6中类的继承机制,特别是如何继承静态属性。通过具体代码示例,展示了子类如何访问父类的静态属性,以及实例方法在原型链中的查找顺序。

被折叠的 条评论
为什么被折叠?



