Extending a class
Extending classes can be used to allow for new behaviors, while adhering to common interfaces. While it's not always the best way to organize relationships between objects, there are many situations where extension (sometimes called inheritance) is the most effective way to structure behavior.
In this recipe, we'll see a very simple example of extension.
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-01-extending-classes
. - Copy or create an
index.html
that loads and runs amain
function frommain.js
. - Create a
main.js
file that defines a new class namedRocket
that takes a constructor argumentname
and assigns it to an instance property:
// main.js class Rocket { constructor(name) { this.name = name; } }
- Create a class named
InactiveRocket...