When working with a large collection, it can be nice to know what kind of object to expect when picking out on a member. Normally, JavaScript collections are heterogeneous, meaning that any type can be used. In the case of Map
, this means that either key
or value
can take on any type.
In this recipe, we'll take a look at how to create a wrapper class for Map
in order to control what types are used in the Map
.
This recipe assumes that you already have a workspace that allows you to create and run ES modules in your browser. If you don't, refer to the first two chapters.
- Open your command-line application and navigate to your workspace.
- Create a new folder named
11-06-create-class-to-wrap-map
.
- Create a
main.js
file that defines a newclass
namedRocket
that takes a constructor argumentname
and assigns it to an instance property:
// main.js class Rocket { constructor(name) { this.name = name...