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

139 lines
5.9 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><strong><code>Symbol.unscopables </code></strong>指用于指定对象值,其对象自身和继承的从关联对象的 with 环境绑定中排除的属性名称。</p>
<div><table class="standard-table">
<thead>
<tr>
<th class="header" colspan="2"><code>Symbol.unscopables</code> 属性的属性特性:</th>
</tr>
</thead>
<tbody>
<tr>
<td>writable</td>
<td>false</td>
</tr>
<tr>
<td>enumerable</td>
<td>false</td>
</tr>
<tr>
<td>configurable</td>
<td>false</td>
</tr>
</tbody>
</table></div>
<h2 id="描述">描述</h2>
<p><code>可以在任何对象上定义 @@unscopables</code> symbol (<code>Symbol.unscopables</code>)<code>,用于排除属性名称并与 with 环境绑定在一起作为词法变量公开。 请注意,如果使用 </code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode">Strict mode</a><code>,语句将不可用,并且可能也不需要 symbol。</code></p>
<p>在 <code>unscopables</code> 对象上设置属性为 true将使其 <em>unscopable</em> 并且因此该属性也将不会在词法环境变量中出现。 如果设置属性为 <code>false</code> ,则将使其可 <code>scopable</code> 并且该属性会出现在词法环境变量中。</p>
<h2 id="示例">示例</h2>
<p>下列的代码可兼容 ES5 及以下版本。然而 ECMAScript 2015 (ES6) 或其后续版本中,<a href="Reference/Global_Objects/Array/keys" title="keys() 方法返回一个包含数组中每个索引键的Array Iterator对象。"><code>Array.prototype.keys()</code></a> 方法才会出现。意味着内部 <code>with</code> 环境“关键字” 存在该方法,但变量中不会存在。 也就是说,当 <code>unscopable</code>s symbol 被展示时,内置的<code>unscopables </code>设置是由 <a href="Reference/Global_Objects/Array/@@unscopables" title="Symbol属性 @@unscopable 包含了所有 ES2015 (ES6) 中新定义的且并未被更早的 ECMAScript 标准收纳的属性名。这些属性并不包含在 with 语句绑定的环境中"><code>Array.prototype[@@unscopables]</code></a> 展示并实现的, 一些 Array 的方法 将作为 scoped 放入 <code>with</code> 语句中。</p>
<pre><code class="language-javascript">var keys = [];
with(Array.prototype) {
keys.push("something");
}
Object.keys(Array.prototype[Symbol.unscopables]);
// ["copyWithin", "entries", "fill", "find", "findIndex",
// "includes", "keys", "values"]
</code></pre>
<p>你也可以为你自己的对象设置 <code>unscopables</code> 。</p>
<pre><code class="language-javascript">var obj = {
foo: 1,
bar: 2
};
obj[Symbol.unscopables] = {
foo: false,
bar: true
};
with(obj) {
console.log(foo); // 1
console.log(bar); // ReferenceError: bar is not defined
}
</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.unscopables" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">Symbol.unscopables</small></a></td>
<td><span class="spec-Standard">Standard</span></td>
<td>首次定义</td>
</tr>
<tr>
<td><a class="external" href="https://tc39.github.io/ecma262/#sec-symbol.unscopables" hreflang="en" lang="en" rel="noopener">ECMAScript Latest Draft (ECMA-262)<br/><small lang="zh-CN">Symbol.unscopables</small></a></td>
<td><span class="spec-Draft">Draft</span></td>
<td><span class="op_dict_text2">起草</span></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/Edge</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td>38</td>
<td><a href="/en-US/Firefox/Releases/48" title="Released on 2016-08-02.">48</a> (48)</td>
<td>12</td>
<td><span style="color: #f00;">未实现</span></td>
<td>9</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/Edge 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>48.0 (48)</td>
<td>12</td>
<td><span style="color: #f00;">未实现</span></td>
<td>9</td>
</tr>
</tbody>
</table>
</div>
<h2 id="相关链接">相关链接</h2>
<ul>
<li><a href="Reference/Global_Objects/Array/@@unscopables" title="Symbol属性 @@unscopable 包含了所有 ES2015 (ES6) 中新定义的且并未被更早的 ECMAScript 标准收纳的属性名。这些属性并不包含在 with 语句绑定的环境中"><code>Array.prototype[@@unscopables]</code></a></li>
<li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/with">with</a></code> statement (not available in <a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">Strict mode</a>)</li>
</ul>
</article>