W3cubDocs

/JavaScript

WeakSet

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 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

Description

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:

  • 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 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.

Properties

WeakSet.length
The value of the length property is 0.
WeakSet.prototype
Represents the prototype for the Set constructor. Allows the addition of properties to all WeakSet 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 return false 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 the WeakSet object.

Specifications

Browser compatibility

Feature 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
Feature Android webview Chrome for Android Edge mobile Firefox for Android IE mobile Opera Android iOS Safari
Basic support 36 36 12 34 No 23 9
new WeakSet(iterable) 38 38 12 34 No 25 9
new WeakSet(null) Yes Yes 12 37 No ? 9
add 36 36 Yes 34 No 23 9
clear 36 — 43 36 — 43 No 34 — 46 No 25 — 30 No
delete 36 36 Yes 34 No 23 9
has 36 36 Yes 34 No 23 9
prototype 36 36 Yes 34 No 23 9

See also

© 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