We've seen how to use Maps in a variety of situations. There is another new class in ECMAScript that behaves in a very similar way, but has some helpful properties. The WeakMap
, like Map
, is a key-value data structure.
In this recipe, we'll take a look at how to add and remove elements from a WeakMap
with the set
and delete
methods. And we'll also see how they differ from the Map
class.
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-07-set-and-delete-from-weakmap
. - 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; } }
- Create an
enum
of various launch sites...