Passing a class as an argument
Classes, like functions, are first class citizens in JavaScript. This means that they can be returned from functions or passed as arguments. In this recipe, we'll see how to use the latter.
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-08-passing-class-as-an-argument. - Copy or create an
index.htmlthat loads and runs amainfunction frommain.js. - Create a
main.jsfile that defines a new class namedRocket:
// main.js
class Rocket {
constructor(name) {
this.name = name;
}
}- Create a class named
InactiveRocketthat extends theRocketclass and assigns anameand alastFlowproperty in the constructor:
// main.js
class InactiveRocket extends Rocket {
constructor(name, lastFlown) {
super();
this.lastFlown = lastFlown...