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
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
@@unscopablesChrome Full support 38Edge Full support 12Firefox Full support 48IE No support NoOpera Full support 25Safari Full support 10WebView Android Full support 38Chrome Android Full support 38Firefox Android Full support 48Opera Android Full support 25Safari iOS Full support 10Samsung Internet Android Full support 3.0nodejs Full support 0.12

Legend

Full support  
Full support
No support  
No support

相关链接