Array.prototype[@@unscopables]
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"]