Creating a WeakSet from existing data
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.
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
12-04-create-weak-set-from-data. - Create a
main.jsfile that defines a new class namedRocketthat takes a constructor argumentnameand assigns it to an instance property:
// main.js
class Rocket {
constructor(name) {
this.name = name;
}
} - Create a
mainfunction with an array of rocket instances. Create a newWeakSetfrom the array. Try to add a string toWeakSet:
// main.js
export function main() { ...