Super
The super keyword is used to access and call functions on an object's parent. The super.prop and super[expr] expressions are legible in any method definition in both classes and object literals. It is used in the "extended" class, which uses "extends" keyword.
syntax
super(arguments);
Example
In the following example, the characteristics of a class called "Person" have been extended to another class called "Student". In both classes, we have used unique properties. Here "super" keyword is used to access a property from parent class(Person) to the extended class(Student), whereas "this" keyword is used to access extended class's own property.
<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>Output
Rishab pant interested in cricket ! Rishab pant wants to become a crickter! Rishab pant needs a cricket kit