Tutorial by Examples

The WeakSet object is used for storing weakly held objects in a collection. The difference from Set is that you can't store primitive values, like numbers or string. Also, references to the objects in the collection are held weakly, which means that if there is no other reference to an object stored...
To add a value to a WeakSet, use the .add() method. This method is chainable. const obj1 = {}, obj2 = {}; const weakset = new WeakSet(); weakset.add(obj1).add(obj2);
To check if a value exits in a WeakSet, use the .has() method. const obj1 = {}, obj2 = {}; const weakset = new WeakSet([obj1]); console.log(weakset.has(obj1)); // true console.log(weakset.has(obj2)); // false
To remove a value from a WeakSet, use the .delete() method. This method returns true if the value existed and has been removed, otherwise false. const obj1 = {}, obj2 = {}; const weakset = new WeakSet([obj1]); console.log(weakset.delete(obj1)); // true console.log(weakset.delete(obj2));...

Page 1 of 1