JS CLASSESChapter 5 · JS Functions and Classes
Class Inheritance
class Comet extends SpaceObject creates a class that inherits every method of SpaceObject. In the child's constructor, call super(...) before you use this. Override a method by defining it again, and reach the parent's version with super.method(). instanceof checks the whole chain of parents.
Worked example
How it reads
extendssets up the parent classsuper(name)runs the parent constructor firstsuper.describe()calls the version the child is overriding
Common mistakes
- Using
thisin a child constructor before callingsuper()throws a ReferenceError.

Cloud tip: Keep inheritance shallow. One or two levels is easy to follow; five is a maze.


