The WeakSet object lets you store weakly held objects in a collection.
Syntax
new WeakSet([iterable]);
Parameters
- iterable
- If an iterable object is passed, all of its elements will be added to the new WeakSet. null is treated as undefined.
Description
WeakSet objects are collections of objects. An object in the WeakSet may only occur once; it is unique in the WeakSet's collection.
The main differences to the Set object are:
- In contrast to Sets,WeakSetsare collections of objects only and not of arbitrary values of any type.
- The WeakSetis weak: References to objects in the collection are held weakly. If there is no other reference to an object stored in theWeakSet, they can be garbage collected. That also means that there is no list of current objects stored in the collection.WeakSetsare not enumerable.
Properties
- WeakSet.length
- The value of the lengthproperty is 0.
- WeakSet.prototype
- Represents the prototype for the Setconstructor. Allows the addition of properties to allWeakSetobjects.
WeakSet instances
All WeakSet instances inherit from WeakSet.prototype.
Properties
- WeakSet.prototype.constructor
- Returns the function that created an instance's prototype. This is the WeakSetfunction by default.
Methods
- WeakSet.prototype.add(value)
- Appends a new object with the given value to the WeakSetobject.
- WeakSet.prototype.delete(value)
- Removes the element associated to the value.WeakSet.prototype.has(value)will returnfalseafterwards.
- WeakSet.prototype.has(value)
- Returns a boolean asserting whether an element is present with the given value in the WeakSetobject or not.
- WeakSet.prototype.clear()
- Removes all elements from the- WeakSetobject.
Examples
Using the WeakSet object
var ws = new WeakSet();
var obj = {};
var foo = {};
ws.add(window);
ws.add(obj);
ws.has(window); // true
ws.has(foo);    // false, foo has not been added to the set
ws.delete(window); // removes window from the set
ws.has(window);    // false, window has been removed
Specifications
| Specification | Status | Comment | 
|---|---|---|
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'WeakSet' in that specification. | Standard | Initial definition. | 
| ECMAScript Latest Draft (ECMA-262) The definition of 'WeakSet' in that specification. | Living Standard | 
Browser compatibility
| Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari | 
|---|---|---|---|---|---|---|
| Basic support | 36 | 12 | 34 (34) | No support | 23 | 9 | 
| new WeakSet(iterable) | 38 | 12 | 34 (34) | No support | 25 | 9 | 
| Constructor argument: new WeakSet(null) | (Yes) | 12 | 37 (37) | No support | ? | 9 | 
| Monkey-patched add()in Constructor | (Yes) | 12 | 37 (37) | No support | ? | 9 | 
| Obsolete clear() method removed | 43 | 12 | 46 (46) | No support | 30 | 9 | 
| Feature | Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | 
|---|---|---|---|---|---|---|
| Basic support | No support | (Yes) | 34.0 (34) | No support | No support | 9 | 
| new WeakMap(iterable) | No support | (Yes) | 34.0 (34) | No support | No support | 9 | 
| Constructor argument: new WeakSet(null) | ? | (Yes) | (Yes) | No support | ? | 9 | 
| Monkey-patched add()in Constructor | ? | (Yes) | (Yes) | No support | ? | 9 | 
| Obsolete clear() method removed | No support | (Yes) | ? | No support | ? | 9 | 
See also
Document Tags and Contributors
    
    Tags: 
    
  
                    
                       Contributors to this page: 
        jameshkramer, 
        kdex, 
        fscholz, 
        David_Gilbertson, 
        danwellman, 
        malyw, 
        Jeremie, 
        m_gol, 
        arai, 
        realityking, 
        claudepache, 
        teoli, 
        phistuck, 
        sanderd17
                    
                    
                       Last updated by:
                      jameshkramer,