Assigning properties with constructor arguments
Now that we have a new class, it's time to start differentiating instances. In this recipe, we'll see how to assign properties when instances are created, via constructor arguments.
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
07-02-assigning-constructor-props
. - Copy or create an
index.html
that loads and runs amain
function frommain.js
. - Create a
main.js
file that creates a new class namedRocket
. Add aconstructor
method that takes a single argument,name
, and assigns it to a property of the same name in the body of the method:
// main.js class Rocket { constructor(name) { this.name = name; } }
- Create a
main
function that creates two instances and logs them out with their property:
// main.js export...