Overriding parent class static methods
We've seen previously that behaviors are not limited to class instances but are also attached to the classes themselves. These static
methods can also be overridden by subclasses.
In this recipe, we'll see how to override static methods.
Getting ready
This recipe assumes you already have a workspace that allows you to create and run ES modules in your browser. If you don't, please see the first two chapters.
How to do it...
- Open your command line application and navigate to your workspace.
- Create a new folder named
08-04-checking-with-instanceof
. - Copy or create an
index.html
that loads and runs amain
function frommain.js
. - Create two objects
rocketMap
andinactiveRocketMap
:
// main.js let rocketMap = {}; let inactiveRocketMap = {};
- Define a new class named
Rocket
. Add a constructor. Use the name to assign the instance to therocketMap
and define a simpleprint
method:
// main.js class Rocket { constructor(name) { this.name = name; rocketMap...