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.html
that loads and runs amain
function frommain.js
. - Create a
main.js
file that defines a new class namedRocket
:
// main.js class Rocket { constructor(name) { this.name = name; } }
- Create a class named
InactiveRocket
that extends theRocket
class and assigns aname
and alastFlow
property in the constructor:
// main.js class InactiveRocket extends Rocket { constructor(name, lastFlown) { super(); this.lastFlown = lastFlown...