Not Just for Classes
super works in any object method declared with the shorthand syntax method() {}. When a method uses this syntax, JS stores a reference to its " home object " ([[HomeObject]]) which super uses to find the parent's method.
let animal = {
sayHi() { console.log('I am an animal'); }
};
let rabbit = {
__proto__: animal,
sayHi() {
super.sayHi(); // Calls animal.sayHi()
console.log('I am also a rabbit');
}
};
rabbit.sayHi();
// "I am an animal"
// "I am also a rabbit"
Caution
super does NOT work when a method uses the function keyword syntax: sayHi: function() { super.sayHi() }. Only the shorthand sayHi() { ... } creates the internal [[HomeObject]] binding.
Important
super is the ONLY reliable way to call an ancestor's method, especially when methods are copied between objects.