Array.prototype[@@unscopables]

Symbol 属性 @@unscopable 包含了所有 ES2015 (ES6) 中新定义的、且并未被更早的 ECMAScript 标准收纳的属性名。这些属性被排除在由 with 语句绑定的环境中。

语法

arr[Symbol.unscopables]

描述

with 绑定中未包含的数组默认属性有:

参考 Symbol.unscopables 以了解如何为自定义的对象设置 unscopables

Array.prototype[@@unscopables] 属性的属性特性:
writable false
enumerable false
configurable true

示例

以下的代码在 ES5 或更早的版本中能正常工作。然而 ECMAScript 2015 (ES6) 或之后的版本中新添加了 Array.prototype.keys() 这个方法。这意味着在 with 语句的作用域中,"keys"只能作为方法,而不能作为某个变量。这正是内置的 @@unscopablesArray.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

浏览器兼容性

Update compatibility data on GitHub
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
@@unscopables Chrome Full support 38 Edge Full support 12 Firefox Full support 48 IE No support No Opera Full support 25 Safari ? WebView Android Full support 38 Chrome Android Full support 38 Firefox Android Full support 48 Opera Android Full support 25 Safari iOS ? Samsung Internet Android Full support 3.0 nodejs Full support 0.12

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown

相关链接