The WeakSet
object lets you store weakly held objects in a collection.
new WeakSet([iterable]);
WeakSet
. null is treated as undefined.var ws = new WeakSet(); var foo = {}; var 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
Note that foo !== bar
. While they are similar objects, they are not the same object. And so they are both added to the set.
WeakSet
objects are collections of objects. An object in the WeakSet
may occur only 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.TODO describe usecases.
WeakSet.length
length
property is 0.WeakSet.prototype
WeakSet
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.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. | Draft |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 36 | 12 | 34 | No | 23 | 9 |
new WeakSet(iterable) |
38 | 12 | 34 | No | 25 | 9 |
new WeakSet(null) |
Yes | 12 | 37 | No | ? | 9 |
add |
36 | Yes | 34 | No | 23 | 9 |
clear
|
36 — 43 | No | 34 — 46 | No | 25 — 30 | No |
delete |
36 | Yes | 34 | No | 23 | 9 |
has |
36 | Yes | 34 | No | 23 | 9 |
prototype |
36 | Yes | 34 | No | 23 | 9 |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | 37 | 36 | 12 | 34 | 23 | 9 | Yes |
new WeakSet(iterable) |
38 | 38 | 12 | 34 | 25 | 9 | Yes |
new WeakSet(null) |
Yes | Yes | 12 | 37 | ? | 9 | Yes |
add |
37 | 36 | Yes | 34 | 23 | 9 | Yes |
clear
|
37 — 43 | 36 — 43 | No | 34 — 46 | 25 — 30 | No | Yes |
delete |
37 | 36 | Yes | 34 | 23 | 9 | Yes |
has |
37 | 36 | Yes | 34 | 23 | 9 | Yes |
prototype |
37 | 36 | Yes | 34 | 23 | 9 | Yes |
Server | |
---|---|
Node.js | |
Basic support | 0.12 |
new WeakSet(iterable) |
0.12 |
new WeakSet(null) |
0.12 |
add |
0.12 |
clear
|
No |
delete |
0.12 |
has |
0.12 |
prototype |
0.12 |
© 2005–2018 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