Chapter XX · Classes
super
The `super` keyword is used to call constructor methods or access parent class properties and functions.
01 How It Works
Think of passing required base materials up to a master supervisor constructor template.
`super()` must be called in a child constructor before using the `this` keyword to ensure parent initialization completes.
example.js
class Parent {
constructor(name) { this.name = name; }
}
class Child extends Parent {
constructor(name, age) {
super(name); // Calls parent constructor
this.age = age;
}
}
1. Extend a parent class.
2. Invoke `super(args)` inside the child constructor.
3. Access parent methods using `super.methodName()`.
02 Practical Example
Here is how you might see this concept applied in real-world code:
practical.js
class Vehicle {
start() { console.log("Vehicle started"); }
}
class Car extends Vehicle {
start() {
super.start();
console.log("Car engine running");
}
}
Key Takeaway
`super` is essential for initializing parent states and invoking parent methods in subclassing.