Chapter XX · Classes
extends
The `extends` keyword is used in class declarations to create a child class that inherits from a parent class.
01 How It Works
Think of building a specialized sports car model that extends standard base car manufacturing specs.
Under the hood, `extends` sets up the prototype chain link between the child constructor and parent constructor.
example.js
class Animal {
eat() { console.log("Eating"); }
}
class Dog extends Animal {
bark() { console.log("Woof"); }
}
1. Declare a child class using `class Child extends Parent`.
2. Call `super()` inside the child constructor.
3. Inherit all parent class methods and properties.
02 Practical Example
Here is how you might see this concept applied in real-world code:
practical.js
class Shape {
constructor(color) { this.color = color; }
}
class Circle extends Shape {
radius = 5;
}
Key Takeaway
The `extends` keyword provides clean syntax for class-based inheritance hierarchies.