The WeakSet
constructor lets you create WeakSet
objects that 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 ws.has(bar); // true, bar is retained
Note that foo !== bar
. While they are similar objects, they are not the same object. And so they are both added to the set.
Specifications
Specification |
---|
ECMAScript (ECMA-262) The definition of 'WeakSet constructor' in that specification. |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Desktop | Mobile | Server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
WeakSet() constructor | 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 | Firefox Android Full support 34 | Opera Android Full support 24 | Safari iOS Full support 9 | Samsung Internet Android Full support 3.0 | 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 | Firefox Android Full support 34 | Opera Android Full support 25 | Safari iOS Full support 9 | Samsung Internet Android Full support 3.0 | nodejs Full support 0.12 |
new WeakSet(null) | Chrome Full support 36 | Edge Full support 12 | Firefox Full support 37 | IE No support No | Opera Full support 23 | Safari Full support 9 | WebView Android Full support 37 | Chrome Android Full support 36 | Firefox Android Full support 37 | Opera Android Full support 24 | Safari iOS Full support 9 | Samsung Internet Android Full support 3.0 | nodejs Full support 0.12 |
Legend
- Full support
- Full support
- No support
- No support