Now, we'll take a look at the corresponding weak data structure, WeakSet
. In this recipe, we'll look at how to add and delete items from a WeakSet
, using the respective instance methods, and at some restrictions on membership.
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
12-03-add-remove-from-weak-set
. - Copy or create an
index.html
that loads and runs amain
function frommain.js
. - 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
main
function with someRocket
instances and aWeakMap
instance. Add and remove the instances from the set:
// main...