2019-04-21 11:50:48 +08:00

112 lines
4.5 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<article id="wikiArticle">
<div></div>
<p><code><strong>set()</strong></code> 方法根据指定的<code>key和</code><code>value在</code> <code>WeakMap对象中添加新/更新元素。</code></p>
<h2 id="语法">语法</h2>
<pre><code class="language-javascript"><code><em>wm</em>.set(key, value);</code></code></pre>
<h3 id="参数">参数</h3>
<dl>
<dt>key</dt>
<dd>必须的。是要在<code>WeakMap</code> 对象中添加/更新元素的key部分。</dd>
<dt>value</dt>
<dd>必须的。是要在<code>WeakMap</code> 对象中添加/更新元素的value部分<code></code></dd>
</dl>
<h3 id="返回值">返回值</h3>
<p><code>该WeakMap对象</code></p>
<h2 id="例子">例子</h2>
<h3 id="使用set方法"><code>使用set方法</code></h3>
<pre><code class="language-javascript">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");
</code></pre>
<h2 id="规范">规范</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><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-weakmap.prototype.set" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">WeakMap.prototype.set</small></a></td>
<td><span class="spec-Standard">Standard</span></td>
<td>Initial definition.</td>
</tr>
<tr>
<td><a class="external" href="https://tc39.github.io/ecma262/#sec-weakmap.prototype.set" hreflang="en" lang="en" rel="noopener">ECMAScript Latest Draft (ECMA-262)<br/><small lang="zh-CN">WeakMap.prototype.set</small></a></td>
<td><span class="spec-Draft">Draft</span></td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容">浏览器兼容</h2>
<p></p><div class="blockIndicator warning"><strong><a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">We're converting our compatibility data into a machine-readable JSON format</a></strong>.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
<strong><a class="new" href="/zh-CN/docs/MDN/Contribute/Structures/Compatibility_tables" rel="nofollow">Find out how you can help!</a></strong></div>
<div class="htab">
<a id="AutoCompatibilityTable" name="AutoCompatibilityTable"></a>
<ul>
<li class="selected"><a>Desktop</a></li>
<li><a>Mobile</a></li>
</ul>
</div><p></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>36</td>
<td><a href="/en-US/Firefox/Releases/6" title="Released on 2011-08-16.">6.0</a> (6.0)</td>
<td>11</td>
<td>23</td>
<td>7.1</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><span style="color: #f00;">未实现</span></td>
<td>6.0 (6.0)</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td>8</td>
</tr>
</tbody>
</table>
</div>
<h2 id="Firefox-特殊说明">Firefox-特殊说明</h2>
<ul>
<li>直到Firefox 33 (Firefox 33 / Thunderbird 33 / SeaMonkey 2.30), <code>WeakMap.prototype.set</code> returned <code>undefined</code> 而不支持联式。它已经修复 (<a class="external" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1031632" rel="noopener" title="FIXED: Map.prototype.set, WeakMap.prototype.set and Set.prototype.add should be chainable">bug 1031632</a>). 这样的实现也存在于 Chrome/v8 (<a class="external" href="https://code.google.com/p/v8/issues/detail?id=3410" rel="noopener">issue</a>).</li>
</ul>
<h2 id="See_also">See also</h2>
<ul>
<li><a href="Reference/WeakMap" title="此页面仍未被本地化, 期待您的翻译!"><code>WeakMap</code></a></li>
<li><a href="Reference/Global_Objects/WeakMap/get" title="get() 方法返回  WeakMap 指定的元素。"><code>WeakMap.prototype.get()</code></a></li>
<li><a href="Reference/Global_Objects/WeakMap/has" title="has() 方法根据WeakMap对象的元素中是否存在key键返回一个boolean值。"><code>WeakMap.prototype.has()</code></a></li>
</ul>
</article>