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.htmlthat loads and runs amainfunction frommain.js. - Create a
main.jsfile that defines a new class namedRocketthat takes a constructor argumentnameand assigns it to an instance property:
// main.js
class Rocket {
constructor(name) {
this.name = name;
}
} - Create a class named
InactiveRocket...