JavaScript中的超级关键字?
超
该超级 关键字用于访问和调用函数对对象的父。在类和对象文字中的任何方法定义中,super.prop和super[expr]表达式均清晰可见。它在“extended”类中使用,该类使用“extends”关键字。
语法
super(arguments);
示例
在以下示例中,名为“人”的类的特征已扩展到名为“学生”的另一个类。在这两个类中,我们都使用了唯一的属性。在这里,“super”关键字用于访问父类(人)到扩展类(学生)的属性,而“this”关键字用于访问扩展类的自身属性。
<html>
<body>
<script>
class Person {
constructor(name, grade) {
this.name = name;
this.grade = grade;
}
goal() {
return `${this.name} wants to become a crickter!`;
}
interest() {
return `${this.name} interested in cricket !`;
}
}
class Student extends Person {
constructor(name, grade) {
super(name, grade);
}
need() {
return `${this.name} needs a cricket kit`;
}
career() {
return `${super.interest()}
${super.goal()}
${this.need()}`;
}
}
const student = new Student('Rishab pant', '7');
document.write(student.career());
</script>
</body>
</html>输出结果
Rishab pant interested in cricket ! Rishab pant wants to become a crickter! Rishab pant needs a cricket kit