The concept of super
keyword comes with the idea of inheritance in JavaScript. Suppose that you have a member variable or method of the same name in the derived class as you do in the base class.
When referring to that variable/method, how would the program know if you are referring to the base class or the derived class?
This is where super
comes into play. The super
keyword in JavaScript acts as a reference variable to the parent class.
Example
You can try to run the following code to implement super()
<!DOCTYPE html>
<html>
<body>
<script>
class Department {
constructor() {}
static msg() {
return 'Hello';
}
}
class Employee extends Department {
constructor() {}
static displayMsg() {
return super.msg() + ' World!';
}
}
document.write(Employee.displayMsg());
</script>
</body>
</html>