Assigning additional properties with constructor arguments
If we are extending a class, we'll want it to be a little bit different. Otherwise, what's the point of extending it? In this recipe we'll differentiate a child class by adding additional properties.
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-02-additional-constructor-args
.
- 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
that extends theRocket
class, and assigns an additionallastFlown
property in the constructor:
// main.js class InactiveRocket extends Rocket { constructor(name, lastFlown...