Back to results for “Design Patterns in JavaScript”
JavaScript › Classes & Class Syntax

How does method overriding with super work in JavaScript classes?

Advanced

Answer

A subclass can redefine a parent's method by declaring one with the same name; calling super.methodName() from inside the override lets you still invoke the parent's original implementation, typically to extend rather than fully replace it.
class Shape {
  describe() { return 'A shape'; }
}
class Circle extends Shape {
  describe() { return super.describe() + ', specifically a circle'; }
}
new Circle().describe(); // 'A shape, specifically a circle'
Real-world example Extending a base Logger class's log() method to add a timestamp prefix while still writing the original message.

Common follow-ups: Can you call super outside of a class method?