Symbol属性 @@unscopable 包含了所有 ES2015 (ES6) 中新定义的且并未被更早的 ECMAScript 标准收纳的属性名。这些属性并不包含在 with
语句绑定的环境中
arr[Symbol.unscopables]
with
绑定中未包含的数组默认属性有:copyWithin, entries, fill, find, findIndex, includes, keys 和 values。
查看 Symbol.unscopables
以了解如何给你定义的对象设置 unscopables。
Array.prototype[@@unscopables] 属性的属性特性: |
|
---|---|
writable | false |
enumerable | false |
configurable | true |
以下的代码在 ES5 或更早的版本中能正常工作。然而 ECMAScript 2015 (ES6) 或之后的版本中新添加了 Array.prototype.keys()
这个方法。这意味着在 with
语句的作用域,"keys"只能作为方法而不能作为某个变量。这正是内置的 @@unscopables
即 Array.prototype[@@unscopables]
symbol属性所要解决的问题:防止某些数组方法被添加到 with 语句的作用域内。
var keys = []; with(Array.prototype) { keys.push("something"); } Object.keys(Array.prototype[Symbol.unscopables]); // ["copyWithin", "entries", "fill", "find", "findIndex", // "includes", "keys", "values"]
规范名称 | 规范状态 | 备注 |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) Array.prototype[@@unscopables] |
Standard | 首次定义 |
ECMAScript Latest Draft (ECMA-262) Array.prototype[@@unscopables] |
Draft |
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@unscopables | Chrome ? | Edge Full support 12 | Firefox Full support 48 | IE ? | Opera ? | Safari ? | WebView Android ? | Chrome Android ? | Edge Mobile ? | Firefox Android Full support 48 | Opera Android ? | Safari iOS ? | Samsung Internet Android ? | nodejs Full support 0.12 |