Overview
Make a child class that reuses its parent's constructor and methods through super, and overrides only what differs.
- class Child extends Parent
- super(...) in the constructor
- super.method() to extend a method
Short Course
A child class must call super(...) before touching this, or you get a
ReferenceError. Inside a method, super.method() runs the parent's version
so you can add to it.
class Animal {
constructor(name) { this.name = name; }
speak() { return `${this.name} makes a sound`; }
}
class Dog extends Animal {
speak() { return `${super.speak()}: woof`; }
}
new Dog("Rex").speak(); // "Rex makes a sound: woof"
Task
Write Circle(radius) extending the given Shape: name "circle", area()
returns π·r² rounded to 2 decimals, and describe() adds ", radius <r>" to
the parent's text, e.g. new Circle(2).describe() is
circle with area 12.57, radius 2.
Test Suites
Pending
Next
Output