Javascript - Inheritance (extends & super)
Code Life
By Pierre Marsaa
A Class Built on Another
Imagine adding a special kind of Codémon: a legendary one, with everything a normal Codémon has plus a signature move.
The naive approach is to copy the whole Codemon class and add one method — but now you have two copies of the same code to keep in sync forever:
↗️
Loading editor...
↶
↷
The extends keyword lets a class inherit everything from another one. class Legendary extends Codemon means: start from Codemon, keep its constructor and all its methods, and only add what's new:
↗️
Loading editor...
↶
↷
Legendary never wrote a constructor or a describe() method, yet both work on titan: they were inherited from Codemon.
In this relationship, Codemon is called the parent class and Legendary the child class.
Verify