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.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
InactiveRocketthat extends theRocketclass, and assigns an additionallastFlownproperty in the constructor:
// main.js
class InactiveRocket extends Rocket {
constructor(name, lastFlown...