JavaScript WeakSet Object

The JavaScript WeakSet object is the type of collection that allows us to store weakly held objects. Unlike Set, the WeakSet are the collections of objects only. It doesn’t contain the arbitrary values.

Syntax

new WeakSet([iterable])  

Parameter

iterable – It represents the iterable object whose elements will be added to a new 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

Let’s see the list of JavaScript WeakSet methods with their description.

MethodsDescription
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.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *