We just saw how to add values individually to a WeakMap
and why it is different from a Map
. However, it can be tedious to create a WeakMap
one entry at a time.
In this recipe, we'll take a look at how to create a new Map
with the preexisting data.
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 WeakMap recipe.
- Open your command-line application and navigate to your workspace.
- Create a new folder named
11-08-create-weakmap-from-data
.
- 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 an enum of various launch sites:
// main.js const LaunchSite = { ...