Symbol.unscopables 指用于指定对象值,其对象自身和继承的从关联对象的 with 环境绑定中排除的属性名称。

Symbol.unscopables 属性的属性特性:
writable false
enumerable false
configurable false

描述

可以在任何对象上定义 @@unscopables symbol (Symbol.unscopables),用于排除属性名称并与 with 环境绑定在一起作为词法变量公开。 请注意,如果使用 Strict mode,语句将不可用,并且可能也不需要 symbol。

在 unscopables 对象上设置属性为 true,将使其 unscopable 并且因此该属性也将不会在词法环境变量中出现。 如果设置属性为 false ,则将使其可 scopable 并且该属性会出现在词法环境变量中。

示例

下列的代码可兼容 ES5 及以下版本。然而,在 ECMAScript 2015 (ES6) 或其后续版本中,Array.prototype.keys() 方法才会出现。意味着内部 with 环境“关键字” 存在该方法,但变量中不会存在。 也就是说,当 unscopables symbol 被展示时,内置的unscopables 设置是由 Array.prototype[@@unscopables] 展示并实现的, 一些 Array 的方法 将作为 scoped 放入 with 语句中。

var keys = [];

with(Array.prototype) {
  keys.push("something");
}

Object.keys(Array.prototype[Symbol.unscopables]); 
// ["copyWithin", "entries", "fill", "find", "findIndex", 
//  "includes", "keys", "values"]

你也可以为你自己的对象设置 unscopables 。

var obj = { 
  foo: 1, 
  bar: 2 
};

obj[Symbol.unscopables] = { 
  foo: false, 
  bar: true 
};

with(obj) {
  console.log(foo); // 1
  console.log(bar); // ReferenceError: bar is not defined
}

规范

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
Symbol.unscopables
Standard 首次定义
ECMAScript Latest Draft (ECMA-262)
Symbol.unscopables
Draft 起草

浏览器兼容性

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!

Feature Chrome Firefox (Gecko) Internet Explorer/Edge Opera Safari
Basic support 38 48 (48) 12 未实现 9
Feature Android Chrome for Android Firefox Mobile (Gecko) IE/Edge Mobile Opera Mobile Safari Mobile
Basic support 未实现 未实现 48.0 (48) 12 未实现 9

相关链接