Our volunteers haven't translated this article into فارسی yet. Join us and help get the job done!
You can also read the article in English (US).
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.
Examples
Using the WeakSet object
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.
Description
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:
- In contrast to
Sets
,WeakSets
are collections of objects only and not of arbitrary values of any type. - The
WeakSet
is 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.WeakSets
are not enumerable.
TODO describe usecases.
Properties
WeakSet.length
- The value of the
length
property is 0. WeakSet.prototype
- Represents the prototype for the
WeakSet
constructor. Allows the addition of properties to allWeakSet
objects.
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
WeakSet
function by default.
Methods
WeakSet.prototype.add(value)
- Appends a new object with the given value to the
WeakSet
object. WeakSet.prototype.delete(value)
- Removes the element associated to the
value
.WeakSet.prototype.has(value)
will returnfalse
afterwards. WeakSet.prototype.has(value)
- Returns a boolean asserting whether an element is present with the given value in the
WeakSet
object or not. WeakSet.prototype.clear()
Removes all elements from theWeakSet
object.
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. |
Draft |
Browser compatibility
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Basic support | Chrome Full support 36 | Edge Full support 12 | Firefox Full support 34 | IE No support No | Opera Full support 23 | Safari Full support 9 | WebView Android Full support 37 | Chrome Android Full support 36 | Edge Mobile Full support 12 | Firefox Android Full support 34 | Opera Android Full support 23 | Safari iOS Full support 9 | Samsung Internet Android Full support Yes | nodejs Full support 0.12 |
new WeakSet(iterable) | Chrome Full support 38 | Edge Full support 12 | Firefox Full support 34 | IE No support No | Opera Full support 25 | Safari Full support 9 | WebView Android Full support 38 | Chrome Android Full support 38 | Edge Mobile Full support 12 | Firefox Android Full support 34 | Opera Android Full support 25 | Safari iOS Full support 9 | Samsung Internet Android Full support Yes | nodejs Full support 0.12 |
new WeakSet(null) | Chrome Full support Yes | Edge Full support 12 | Firefox Full support 37 | IE No support No | Opera ? | Safari Full support 9 | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support 12 | Firefox Android Full support 37 | Opera Android ? | Safari iOS Full support 9 | Samsung Internet Android Full support Yes | nodejs Full support 0.12 |
add | Chrome Full support 36 | Edge Full support Yes | Firefox Full support 34 | IE No support No | Opera Full support 23 | Safari Full support 9 | WebView Android Full support 37 | Chrome Android Full support 36 | Edge Mobile Full support Yes | Firefox Android Full support 34 | Opera Android Full support 23 | Safari iOS Full support 9 | Samsung Internet Android Full support Yes | nodejs Full support 0.12 |
clear | Chrome No support 36 — 43 | Edge No support No | Firefox No support 34 — 46 | IE No support No | Opera No support 25 — 30 | Safari No support No | WebView Android No support 37 — 43 | Chrome Android No support 36 — 43 | Edge Mobile No support No | Firefox Android No support 34 — 46 | Opera Android No support 25 — 30 | Safari iOS No support No | Samsung Internet Android Full support Yes | nodejs No support No |
delete | Chrome Full support 36 | Edge Full support Yes | Firefox Full support 34 | IE No support No | Opera Full support 23 | Safari Full support 9 | WebView Android Full support 37 | Chrome Android Full support 36 | Edge Mobile Full support Yes | Firefox Android Full support 34 | Opera Android Full support 23 | Safari iOS Full support 9 | Samsung Internet Android Full support Yes | nodejs Full support 0.12 |
has | Chrome Full support 36 | Edge Full support Yes | Firefox Full support 34 | IE No support No | Opera Full support 23 | Safari Full support 9 | WebView Android Full support 37 | Chrome Android Full support 36 | Edge Mobile Full support Yes | Firefox Android Full support 34 | Opera Android Full support 23 | Safari iOS Full support 9 | Samsung Internet Android Full support Yes | nodejs Full support 0.12 |
prototype | Chrome Full support 36 | Edge Full support Yes | Firefox Full support 34 | IE No support No | Opera Full support 23 | Safari Full support 9 | WebView Android Full support 37 | Chrome Android Full support 36 | Edge Mobile Full support Yes | Firefox Android Full support 34 | Opera Android Full support 23 | Safari iOS Full support 9 | Samsung Internet Android Full support Yes | nodejs Full support 0.12 |
Legend
- Full support
- Full support
- No support
- No support
- Compatibility unknown
- Compatibility unknown
- Non-standard. Expect poor cross-browser support.
- Non-standard. Expect poor cross-browser support.
- Deprecated. Not for use in new websites.
- Deprecated. Not for use in new websites.