The WeakSet
object lets you store weakly held objects in a collection.
new WeakSet([iterable]);
WeakSet
. null is treated as undefined.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:
Sets
, WeakSets
are collections of objects only and not of arbitrary values of any type.WeakSet
is weak: References to objects in the collection are held weakly. If there is no other reference to an object stored in the WeakSet
, they can be garbage collected. That also means that there is no list of current objects stored in the collection. WeakSets
are not enumerable.WeakSet.length
length
property is 0.WeakSet.prototype
Set
constructor. Allows the addition of properties to all WeakSet
objects.WeakSet
instancesAll WeakSet
instances inherit from WeakSet.prototype
.
WeakSet.prototype.constructor
WeakSet
function by default.WeakSet.prototype.add(value)
WeakSet
object.WeakSet.prototype.delete(value)
value
. WeakSet.prototype.has(value)
will return false
afterwards.WeakSet.prototype.has(value)
WeakSet
object or not.WeakSet.prototype.clear()
WeakSet
object.WeakSet
objectvar 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
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'WeakSet' in that specification. | Standard | Initial definition. |
ECMAScript 2017 Draft (ECMA-262) The definition of 'WeakSet' in that specification. | Draft |
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 | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | No support | 34.0 (34) | No support | No support | 9 |
new WeakMap(iterable) | No support | 34.0 (34) | No support | No support | 9 |
Constructor argument: new WeakSet(null)
| ? | (Yes) | No support | ? | 9 |
Monkey-patched add() in Constructor | ? | (Yes) | No support | ? | 9 |
Obsolete clear() method removed | No support | ? | No support | ? | 9 |
© 2005–2017 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet