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

107 lines
5.1 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.

This file contains Unicode characters that might be confused with other characters. 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>get()</strong></code> 方法返回  <code>WeakMap</code> 指定的元素。</p>
<h2 id="语法">语法</h2>
<pre><code class="language-javascript"><code><em>wm</em>.get(key);</code></code></pre>
<h3 id="参数">参数</h3>
<dl>
<dt>key</dt>
<dd>必须。 想要从 <code>WeakMap </code>获取的元素的键。</dd>
</dl>
<h3 id="返回值">返回值</h3>
<h3 id="返回与指定键相关联的值如果_WeakMap_对象找不到这个键则返回_undefined。"><span style="font-size: 14px; line-height: 1.5;">返回与指定键相关联的值,如果 </span><code style="font-size: 14px; font-style: normal; font-weight: normal; line-height: 1.5;">WeakMap</code><span style="font-size: 14px; line-height: 1.5;"> 对象找不到这个键则返回 </span><code style="font-size: 14px; font-style: normal; font-weight: normal; line-height: 1.5;">undefined</code><span style="font-size: 14px; line-height: 1.5;"></span></h3>
<h2 id="例子">例子</h2>
<h3 id="使用_get_方法">使用 <code>get </code>方法<code> </code></h3>
<pre><code class="language-javascript">var wm = new WeakMap();
wm.set(window, "foo");
wm.get(window); // 返回 "foo".
wm.get("baz"); // 返回 undefined.
</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.get" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">WeakMap.prototype.get</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.get" hreflang="en" lang="en" rel="noopener">ECMAScript Latest Draft (ECMA-262)<br/><small lang="zh-CN">WeakMap.prototype.get</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>Prior to SpiderMonkey 38 (Firefox 38 / Thunderbird 38 / SeaMonkey 2.35), this method threw a <a href="Reference/Global_Objects/TypeError" title="TypeError类型错误 对象用来表示值的类型非预期类型时发生的错误。"><code>TypeError</code></a> when the key parameter was not an object. However, the latest ES6 standard specifies to return <code>undefined</code> instead. Furthermore, <code>WeakMap.prototype.get</code> accepted an optional second argument as a fallback value, which is not part of the standard. Both non-standard behaviors are removed in version 38 and higher (<a class="external" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1127827" rel="noopener" title="FIXED: WeakMap.get, has and delete should not throw when key param is not an object">bug 1127827</a>).</li>
</ul>
<h2 id="相关链接">相关链接</h2>
<ul>
<li><a href="Reference/WeakMap" title="此页面仍未被本地化, 期待您的翻译!"><code>WeakMap</code></a></li>
<li><a href="Reference/Global_Objects/WeakMap/set" title="set() 方法根据指定的key和value在 WeakMap对象中添加新/更新元素。"><code>WeakMap.set()</code></a></li>
<li><a href="Reference/Global_Objects/WeakMap/has" title="has() 方法根据WeakMap对象的元素中是否存在key键返回一个boolean值。"><code>WeakMap.has()</code></a></li>
</ul>
</article>