mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-09 23:44:06 +08:00
133 lines
5.2 KiB
HTML
133 lines
5.2 KiB
HTML
<article id="wikiArticle">
|
||
<div> </div>
|
||
<p><code><strong>Symbol.for(key)</strong></code> 方法会根据给定的键 <code>key</code>,来从运行时的 symbol 注册表中找到对应的 symbol,如果找到了,则返回它,否则,新建一个与该键关联的 symbol,并放入全局 symbol 注册表中。</p>
|
||
<h2 id="Syntax" name="Syntax">语法</h2>
|
||
<pre><code class="language-javascript"><var>Symbol.for(key)</var>;</code></pre>
|
||
<h3 id="参数">参数</h3>
|
||
<dl>
|
||
<dt>key</dt>
|
||
<dd>一个字符串,作为 symbol 注册表中与某 symbol 关联的键(同时也会作为该 symbol 的描述)。</dd>
|
||
</dl>
|
||
<h3 id="返回值">返回值</h3>
|
||
<p>返回由给定的 key 找到的 symbol,否则就是返回新创建的 symbol。</p>
|
||
<h2 id="Description" name="Description">描述</h2>
|
||
<p>和 <code>Symbol()</code> 不同的是,用 <code>Symbol.for()</code> 方法创建的的 symbol 会被放入一个全局 symbol 注册表中。<code>Symbol.for() 并不是每次都会创建一个新的 symbol</code>,它会首先检查给定的 key 是否已经在注册表中了。假如是,则会直接返回上次存储的那个。否则,它会再新建一个。</p>
|
||
<h3 id="全局_symbol_注册表">全局 symbol 注册表</h3>
|
||
<p>symbol 注册表中的记录结构:</p>
|
||
<table class="standard-table">
|
||
<caption>全局 symbol 注册表中的一个记录</caption>
|
||
<tbody>
|
||
<tr>
|
||
<th>字段名</th>
|
||
<th>字段值</th>
|
||
</tr>
|
||
<tr>
|
||
<td>[[key]]</td>
|
||
<td>一个字符串,用来标识每个 symbol</td>
|
||
</tr>
|
||
<tr>
|
||
<td>[[symbol]]</td>
|
||
<td>存储的 symbol 值</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<h2 id="示例">示例</h2>
|
||
<pre><code class="language-javascript">Symbol.for("foo"); // 创建一个 symbol 并放入 symbol 注册表中,键为 "foo"
|
||
Symbol.for("foo"); // 从 symbol 注册表中读取键为"foo"的 symbol
|
||
|
||
|
||
Symbol.for("bar") === Symbol.for("bar"); // true,证明了上面说的
|
||
Symbol("bar") === Symbol("bar"); // false,Symbol() 函数每次都会返回新的一个 symbol
|
||
|
||
|
||
var sym = Symbol.for("mario");
|
||
sym.toString();
|
||
// "Symbol(mario)",mario 既是该 symbol 在 symbol 注册表中的键名,又是该 symbol 自身的描述字符串
|
||
</code></pre>
|
||
<p>为了防止冲突,最好给你要放入 symbol 注册表中的 symbol 带上键前缀。</p>
|
||
<pre><code class="language-javascript">Symbol.for("mdn.foo");
|
||
Symbol.for("mdn.bar");
|
||
</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-symbol.for" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">Symbol.for</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-symbol.for" hreflang="en" lang="en" rel="noopener">ECMAScript Latest Draft (ECMA-262)<br/><small lang="zh-CN">Symbol.for</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>40</td>
|
||
<td><a href="/en-US/Firefox/Releases/36" title="Released on 2015-02-24.">36.0</a> (36.0)</td>
|
||
<td><span style="color: #f00;">未实现</span></td>
|
||
<td><span style="color: #f00;">未实现</span></td>
|
||
<td>10</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<div id="compat-mobile">
|
||
<table class="compat-table">
|
||
<tbody>
|
||
<tr>
|
||
<th>Feature</th>
|
||
<th>Android</th>
|
||
<th>Chrome for 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><span style="color: #f00;">未实现</span></td>
|
||
<td>36.0 (36.0)</td>
|
||
<td><span style="color: #f00;">未实现</span></td>
|
||
<td><span style="color: #f00;">未实现</span></td>
|
||
<td><span style="color: #f00;">未实现</span></td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<h2 id="See_Also" name="See_Also">相关链接</h2>
|
||
<ul>
|
||
<li><a href="Reference/Global_Objects/Symbol/keyFor" title="Symbol.keyFor(sym) 方法用来获取 symbol 注册表中与某个 symbol 关联的键。"><code>Symbol.keyFor()</code></a></li>
|
||
</ul>
|
||
</article> |