The WeakSet object in JavaScript is a specialized collection designed for storing objects with weak references. In contrast to the Set, which can contain a variety of values, a WeakSet is exclusively for holding objects. It does not permit the inclusion of arbitrary data types.
Syntax
Example
new WeakSet([iterable])
Parameter
iterable - This refers to the iterable object containing elements that will be incorporated into a newly created WeakSet.
Points to remember
- A WeakSet object contains unique objects only.
- In WeakSet, if there is no reference to a stored object, they are targeted to garbage collection.
- In WeakSet, the objects are not enumerable. So, it doesn't provide any method to get the specified objects.
JavaScript WeakSet Methods
Here is a compilation of the methods available in JavaScript's WeakSet, along with their explanations.
| Methods | Description |
|---|---|
| add() | It adds a new object to the end of WeakSet object. |
| delete() | It removes the specified object from the WeakSet object. |
| has() | It indicates whether the WeakSet object contains the specified object element. |