July 2020
Intermediate to advanced
608 pages
15h 43m
English
In this chapter, you'll learn about ES2015+'s Maps, Sets, WeakMaps, and WeakSets. Maps store key/value pairs, where the key and value can be (almost) anything. Sets store unique values. WeakMaps are similar to Maps, but the keys are objects and those objects are held weakly (they can be garbage collected, which removes the entry from the map). WeakSets hold unique objects weakly.
You often want to create a map of one thing to another, for instance a map of IDs to objects that have that ID. In JavaScript, we've often used (some say abused) objects for that. But since it's not what objects are designed for, there are some pragmatic issues with using objects for generic maps:
Read now
Unlock full access