Site icon i2tutorials

Javascript-WeakSet

Javascript-WeakSet

 

WeakSet objects are collections of objects. In the WeakSet object, each object in a WeakSet may occur only once. Only all objects in a WeakSet’s collection are unique.

 

Syntax:

 

new WeakSet([iterable])

 

 

Instance methods:

 

 

 

Using the WeakSet object:

 

const ws = new WeakSet();
const foo = {};
const bar = {};

ws.add(foo);
ws.add(bar);

ws.has(foo);    // true
ws.has(bar);    // true

ws.delete(foo); // removes foo from the set
ws.has(foo);    // false, foo has been removed
ws.has(bar);    // true, bar is retained
Exit mobile version