{{JSRef("Global_Objects", "WeakSet")}} {{ harmony() }}
Summary
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.
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 {{jsxref("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.
Properties
WeakSet.length
- The value of the
length
property is 0. - {{jsxref("WeakSet.prototype")}}
- Represents the prototype for the
Set
constructor. Allows the addition of properties to allWeakSet
objects.
WeakSet
instances
All WeakSet
instances inherit from {{jsxref("WeakSet.prototype")}}.
Properties
{{page('en-US/Web/JavaScript/Reference/Global_Objects/WeakSet/prototype','Properties')}}
Methods
{{page('en-US/Web/JavaScript/Reference/Global_Objects/WeakSet/prototype','Methods')}}
Examples
Example: 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 ws.clear(); // empty the whole WeakSet
Specifications
Specification | Status | Comment |
---|---|---|
{{SpecName('ES6', '#sec-weakset-objects', 'WeakSet')}} | {{Spec2('ES6')}} | Initial definition. |
Browser compatibility
{{ CompatibilityTable() }}
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | {{ CompatChrome(36) }} | {{ CompatGeckoDesktop(34) }} | {{ CompatNo() }} | {{ CompatOpera(23) }} | {{ CompatNo() }} |
new WeakSet(iterable) |
38 | {{ CompatGeckoDesktop(34) }} | {{ CompatNo }} | 25 | {{ CompatNo }} |
Constructor argument: new WeakSet(null) |
{{ CompatVersionUnknown() }} | {{CompatGeckoDesktop("37")}} | {{ CompatUnknown() }} | {{ CompatUnknown() }} | {{ CompatUnknown() }} |
Monkey-patched add() in Constructor |
{{ CompatVersionUnknown() }} | {{CompatGeckoDesktop("37")}} | {{ CompatUnknown() }} | {{ CompatUnknown() }} | {{ CompatUnknown() }} |
Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | {{ CompatNo() }} | {{ CompatGeckoMobile(34) }} | {{ CompatNo() }} | {{ CompatNo() }} | {{ CompatNo() }} |
new WeakMap(iterable) |
{{ CompatNo() }} | {{ CompatGeckoMobile(34) }} | {{ CompatNo }} | {{ CompatNo }} | {{ CompatNo }} |
Constructor argument: new WeakSet(null) |
{{ CompatUnknown() }} | {{ CompatVersionUnknown() }} | {{ CompatUnknown() }} | {{ CompatUnknown() }} | {{ CompatUnknown() }} |
Monkey-patched add() in Constructor |
{{ CompatUnknown() }} | {{ CompatVersionUnknown() }} | {{ CompatUnknown() }} | {{ CompatUnknown() }} | {{ CompatUnknown() }} |
See also
- {{jsxref("Map")}}
- {{jsxref("Set")}}
- {{jsxref("WeakMap")}}