Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 784735 of WeakSet

  • Revision slug: Web/JavaScript/Reference/Global_Objects/WeakSet
  • Revision title: WeakSet
  • Revision id: 784735
  • Created:
  • Creator: m_gol
  • Is current revision? No
  • Comment WeakSet.length is 0 per latest spec

Revision Content

{{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 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.
{{jsxref("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 {{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() }} {{CompatGeckoMobile("37")}} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}
Monkey-patched add() in Constructor {{ CompatUnknown() }} {{ CompatVersionUnknown() }} {{CompatGeckoMobile("37")}} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}

See also

  • {{jsxref("Map")}}
  • {{jsxref("Set")}}
  • {{jsxref("WeakMap")}}

Revision Source

<div>{{JSRef("Global_Objects", "WeakSet")}} {{ harmony() }}</div>

<h2 id="Summary">Summary</h2>

<p>The <strong><code>WeakSet</code></strong> object lets you store weakly held <em>objects</em> in a collection.</p>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">
 new WeakSet([iterable]);</pre>

<h3 id="Parameters">Parameters</h3>

<dl>
 <dt>iterable</dt>
 <dd>If an <a href="/en-US/docs/Web/JavaScript/Reference/Statements/for...of">iterable object </a>is passed, all of its elements will be added to the new <code>WeakSet</code>. null is treated as undefined.</dd>
</dl>

<h2 id="Description">Description</h2>

<p><code>WeakSet</code> objects are collections of objects. An object in the <code>WeakSet</code> may only occur once; it is unique in the <code>WeakSet</code>'s collection.</p>

<p>The main differences to the {{jsxref("Set")}} object are:</p>

<ul>
 <li>In contrast to <code>Sets</code>, <code>WeakSets</code> are <strong>collections of objects only</strong> and not of arbitrary values of any type.</li>
 <li>The <code>WeakSet</code> is <em>weak</em>: References to objects in the collection are held weakly. If there is no other reference to an object stored in the <code>WeakSet</code>, they can be garbage collected. That also means that there is no list of current objects stored in the collection. <code>WeakSets</code> are not enumerable.</li>
</ul>

<h2 id="Properties" name="Properties">Properties</h2>

<dl>
 <dt><code>WeakSet.length</code></dt>
 <dd>The value of the <code>length</code> property is 0.</dd>
 <dt>{{jsxref("WeakSet.prototype")}}</dt>
 <dd>Represents the prototype for the <code>Set</code> constructor. Allows the addition of properties to all <code>WeakSet</code> objects.</dd>
</dl>

<h2 id="Boolean_instances" name="Boolean_instances"><code>WeakSet</code> instances</h2>

<p>All <code>WeakSet</code> instances inherit from {{jsxref("WeakSet.prototype")}}.</p>

<h3 id="Properties_2">Properties</h3>

<p>{{page('en-US/Web/JavaScript/Reference/Global_Objects/WeakSet/prototype','Properties')}}</p>

<h3 id="Methods">Methods</h3>

<p>{{page('en-US/Web/JavaScript/Reference/Global_Objects/WeakSet/prototype','Methods')}}</p>

<h2 id="Examples">Examples</h2>

<h3 id="Example.3A_Using_the_WeakSet_object">Example: Using the <code>WeakSet</code> object</h3>

<pre class="brush: js">
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
</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-weakset-objects', 'WeakSet')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{ CompatibilityTable() }}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{ CompatChrome(36) }}</td>
   <td>{{ CompatGeckoDesktop(34) }}</td>
   <td>{{ CompatNo() }}</td>
   <td>{{ CompatOpera(23) }}</td>
   <td>{{ CompatNo() }}</td>
  </tr>
  <tr>
   <td><code>new WeakSet(iterable)</code></td>
   <td>38</td>
   <td>{{ CompatGeckoDesktop(34) }}</td>
   <td>{{ CompatNo }}</td>
   <td>25</td>
   <td>{{ CompatNo }}</td>
  </tr>
  <tr>
   <td>Constructor argument: <code>new WeakSet(null)</code></td>
   <td>{{ CompatVersionUnknown() }}</td>
   <td>{{CompatGeckoDesktop("37")}}</td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{ CompatUnknown() }}</td>
  </tr>
  <tr>
   <td>Monkey-patched <code>add()</code> in Constructor</td>
   <td>{{ CompatVersionUnknown() }}</td>
   <td>{{CompatGeckoDesktop("37")}}</td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{ CompatUnknown() }}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{ CompatNo() }}</td>
   <td>{{ CompatGeckoMobile(34) }}</td>
   <td>{{ CompatNo() }}</td>
   <td>{{ CompatNo() }}</td>
   <td>{{ CompatNo() }}</td>
  </tr>
  <tr>
   <td><code>new WeakMap(iterable)</code></td>
   <td>{{ CompatNo() }}</td>
   <td>{{ CompatGeckoMobile(34) }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
  </tr>
  <tr>
   <td>Constructor argument: <code>new WeakSet(null)</code></td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{ CompatVersionUnknown() }}</td>
   <td>{{CompatGeckoMobile("37")}}</td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{ CompatUnknown() }}</td>
  </tr>
  <tr>
   <td>Monkey-patched <code>add()</code> in Constructor</td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{ CompatVersionUnknown() }}</td>
   <td>{{CompatGeckoMobile("37")}}</td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{ CompatUnknown() }}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{jsxref("Map")}}</li>
 <li>{{jsxref("Set")}}</li>
 <li>{{jsxref("WeakMap")}}</li>
</ul>
Revert to this revision