We just saw how to create a Set from existing data. The related class WeakSet
can be created in a similar way, but has restrictions on membership. In this recipe, we'll take a look at how to create a WeakSet
from existing data and 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-04-create-weak-set-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 a
main
function with an array of rocket instances. Create a newWeakSet
from the array. Try to add a string toWeakSet
:
// main.js export function main() { ...