Setting and deleting entries from WeakMap
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.
Getting ready
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.
How to do it...
- 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.jsfile that defines a newclassnamedRocketthat takes a constructor argumentnameand assigns it to an instance property:
// main.js
class Rocket {
constructor(name) {
this.name = name;
}
} - Create an
enumof various launch sites...