W3cubDocs

/JavaScript

WeakSet.weakSet

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

Browser compatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
WeakSet() constructor 36 12 34 No 23 9
new WeakSet(iterable) 38 12 34 No 25 9
new WeakSet(null) 36 12 37 No 23 9
Mobile
Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
WeakSet() constructor 37 36 34 24 9 3.0
new WeakSet(iterable) 38 38 34 25 9 3.0
new WeakSet(null) 37 36 37 24 9 3.0
Server
Node.js
WeakSet() constructor 0.12
new WeakSet(iterable) 0.12
new WeakSet(null) 0.12

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet/WeakSet