Knowing what types we are working with is valuable. WeakSet
has some restriction on membership, but as you may know, objects can vary quite a lot.
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.
If you are unfamiliar with the WeakMap
class, refer to the Setting and deleting entries from a WeakMap recipe.
- Open your command-line application, and navigate to your workspace.
- Create a new folder named
12-08-create-class-to-wrap-set
.
- Create a
main.js
file that defines a new class namedRocket
that takes a constructor argumentname
and assigns it to an instance property:
// main.js class Rocket { constructor(name) { this.name = name; } }
- Create a class
RocketSet...