Revision 639599 of WeakMap.prototype.set()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/WeakMap/set
  • Revision title: WeakMap.prototype.set()
  • Revision id: 639599
  • Created:
  • Creator: ziyunfei
  • Is current revision? No
  • Comment

Revision Content

{{JSRef("Global_Objects", "WeakMap")}} {{harmony}}

Summary

The set() method adds a new element with a specified key and value to a WeakMap object.

Syntax

wm.set(key, value);

Parameters

key
Required. The key of the element to add to the WeakMap object.
value
Required. The value of the element to add to the WeakMap object.

Return value

The WeakMap object.

Examples

Example: Using the set method

var wm = new WeakMap();
var obj = {};

// Add new elements to the WeakMap
wm.set(obj, "foo").set(window, "bar"); // chainable

// Update an element in the WeakMap
wm.set(obj, "baz");

Specifications

Specification Status Comment
{{SpecName('ES6', '#sec-weakmap.prototype.set', 'WeakMap.prototype.set')}} {{Spec2('ES6')}} Initial definition.

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (SpiderMonkey) Internet Explorer Opera Safari
Basic support {{CompatVersionUnknown}}[1] {{CompatGeckoDesktop("6.0")}} 11 {{CompatNo}} {{CompatNo}}
Feature Android Firefox Mobile (SpiderMonkey) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatNo}} {{CompatGeckoMobile("6.0")}} {{CompatNo}} {{CompatNo}} {{CompatNo}}

[1] The feature is available behind a pref. Go to chrome://flags and activate the entry “Enable Experimental JavaScript”.

Firefox-specific notes

  • Prior to Firefox 33 {{geckoRelease("33")}}, WeakMap.prototype.set returned undefined and was not chainable. This has been fixed ({{bug(1031632)}}). The behavior can be found in Chrome/v8 as well (issue).

See also

  • {{jsxref("WeakMap")}}
  • {{jsxref("WeakMap.prototype.get()")}}
  • {{jsxref("WeakMap.prototype.has()")}}

Revision Source

<div>
 {{JSRef("Global_Objects", "WeakMap")}} {{harmony}}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>The <code><strong>set()</strong></code> method adds a new element with a specified <code>key</code> and <code>value</code> to a <code>WeakMap</code> object.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code><em>wm</em>.set(key, value);</code></pre>
<h3 id="Parameters">Parameters</h3>
<dl>
 <dt>
  key</dt>
 <dd>
  Required. The key of the element to add to the <code>WeakMap</code> object.</dd>
 <dt>
  value</dt>
 <dd>
  Required. The value of the element to add to the <code>WeakMap</code> object.</dd>
</dl>
<h3 id="Return_value">Return value</h3>
<p>The <code>WeakMap</code> object.</p>
<h2 id="Examples" name="Examples">Examples</h2>
<h3 id="Example:_Testing_size_of_all_array_elements" name="Example:_Testing_size_of_all_array_elements">Example: Using the <code>set</code> method</h3>
<pre class="brush: js">
var wm = new WeakMap();
var obj = {};

// Add new elements to the WeakMap
wm.set(obj, "foo").set(window, "bar"); // chainable

// Update an element in the WeakMap
wm.set(obj, "baz");
</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-weakmap.prototype.set', 'WeakMap.prototype.set')}}</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 (SpiderMonkey)</th>
    <th>Internet Explorer</th>
    <th>Opera</th>
    <th>Safari</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{CompatVersionUnknown}}[1]</td>
    <td>{{CompatGeckoDesktop("6.0")}}</td>
    <td>11</td>
    <td>{{CompatNo}}</td>
    <td>{{CompatNo}}</td>
   </tr>
  </tbody>
 </table>
</div>
<div id="compat-mobile">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Android</th>
    <th>Firefox Mobile (SpiderMonkey)</th>
    <th>IE Mobile</th>
    <th>Opera Mobile</th>
    <th>Safari Mobile</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{CompatNo}}</td>
    <td>{{CompatGeckoMobile("6.0")}}</td>
    <td>{{CompatNo}}</td>
    <td>{{CompatNo}}</td>
    <td>{{CompatNo}}</td>
   </tr>
  </tbody>
 </table>
</div>
<p>[1] The feature is available behind a pref. Go to <code>chrome://flags</code> and activate the entry “Enable Experimental JavaScript”.</p>
<h3 id="Firefox-specific_notes">Firefox-specific notes</h3>
<ul>
 <li>Prior to Firefox 33 {{geckoRelease("33")}}, <code>WeakMap.prototype.set</code> returned <code>undefined</code> and was not chainable. This has been fixed ({{bug(1031632)}}). The behavior can be found in Chrome/v8 as well (<a href="https://code.google.com/p/v8/issues/detail?id=3410">issue</a>).</li>
</ul>
<h2 id="See_also">See also</h2>
<ul>
 <li>{{jsxref("WeakMap")}}</li>
 <li>{{jsxref("WeakMap.prototype.get()")}}</li>
 <li>{{jsxref("WeakMap.prototype.has()")}}</li>
</ul>
Revert to this revision