1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
---
title: 'Array.prototype[@@unscopables]'
slug: Web/JavaScript/Reference/Global_Objects/Array/@@unscopables
tags:
- Array
- ECMAScript 2015
- JavaScript
- Property
- Prototype
translation_of: Web/JavaScript/Reference/Global_Objects/Array/@@unscopables
---
<div>{{JSRef}}</div>
<p>Symbol 属性 <code><strong>@@unscopable</strong></code> 包含了所有 ES2015 (ES6) 中新定义的、且并未被更早的 ECMAScript 标准收纳的属性名。这些属性被排除在由 <code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/with">with</a></code> 语句绑定的环境中。</p>
<h2 id="语法">语法</h2>
<pre class="brush: js"><var>arr</var>[Symbol.unscopables]</pre>
<h2 id="描述">描述</h2>
<p><code>with</code> 绑定中未包含的数组默认属性有:</p>
<ul>
<li>{{jsxref("Array.prototype.copyWithin()", "copyWithin()")}}</li>
<li>{{jsxref("Array.prototype.entries()", "entries()")}}</li>
<li>{{jsxref("Array.prototype.fill()", "fill()")}}</li>
<li>{{jsxref("Array.prototype.find()", "find()")}}</li>
<li>{{jsxref("Array.prototype.findIndex()", "findIndex()")}}</li>
<li>{{jsxref("Array.prototype.includes()", "includes()")}}</li>
<li>{{jsxref("Array.prototype.keys()", "keys()")}}</li>
<li>{{jsxref("Array.prototype.values()", "values()")}}</li>
</ul>
<p>参考 {{jsxref("Symbol.unscopables")}} 以了解如何为自定义的对象设置 <code>unscopables</code>。</p>
<p>{{js_property_attributes(0,0,1)}}</p>
<h2 id="示例">示例</h2>
<p>以下的代码在 ES5 或更早的版本中能正常工作。然而 ECMAScript 2015 (ES6) 或之后的版本中新添加了 {{jsxref("Array.prototype.keys()")}} 这个方法。这意味着在 <code>with</code> 语句的作用域中,"keys"只能作为方法,而不能作为某个变量。这正是内置的 <code>@@unscopables</code> 即 <code>Array.prototype[@@unscopables]</code> symbol 属性所要解决的问题:防止某些数组方法被添加到 <code>with</code> 语句的作用域内。</p>
<pre class="brush: js">var keys = [];
with(Array.prototype) {
keys.push("something");
}
Object.keys(Array.prototype[Symbol.unscopables]);
// ["copyWithin", "entries", "fill", "find", "findIndex",
// "includes", "keys", "values"]</pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">规范名称</th>
<th scope="col">规范状态</th>
<th scope="col">备注</th>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-array.prototype-@@unscopables', 'Array.prototype[@@unscopables]')}}</td>
<td>{{Spec2('ES6')}}</td>
<td>首次定义</td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-array.prototype-@@unscopables', 'Array.prototype[@@unscopables]')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<div>
<p>{{Compat("javascript.builtins.Array.@@unscopables")}}</p>
</div>
<h2 id="相关链接">相关链接</h2>
<ul>
<li>{{jsxref("Symbol.unscopables")}}</li>
</ul>
|