WeakSet
A set of objects that does not prevent their garbage collection.
Available since: ES2015
Syntax
new WeakSet(iterable)Parameters
| Parameter | Type | Description |
|---|---|---|
| iterable | Iterable | Optional. Objects to start with. |
Return value
WeakSetA new set of objects.
Examples
A processed mark:
const processed = new WeakSet();
const order = { id: 7 };
processed.add(order);
console.log(processed.has(order));
console.log(processed.has({ id: 7 }));Output:
true
falseNotes
It stores only objects, has no size and cannot be iterated. It suits marking objects without adding properties to them.
See also
In the tutorial
- Map and Set — The Map and Set collections in JavaScript: a dictionary with keys of any type and a set of unique values.