Array.prototype[Symbol.unscopables]

Array.prototype[Symbol.unscopables] 数据属性由所有 Array 实例共享。它包含 ES2015 版本之前的 ECMAScript 标准中未包含的属性名称,并且出于 with 语句绑定目的而被忽略。

¥The [Symbol.unscopables] data property of Array.prototype is shared by all Array instances. It contains property names that were not included in the ECMAScript standard prior to the ES2015 version and that are ignored for with statement-binding purposes.

¥Value

null-原型对象 的属性名称如下所示,其值设置为 true

¥A null-prototype object with property names given below and their values set to true.

Property attributes of Array.prototype[Symbol.unscopables]
Writable no
Enumerable no
Configurable yes

描述

¥Description

出于 with 语句绑定目的而忽略的默认 Array 属性是:

¥The default Array properties that are ignored for with statement-binding purposes are:

Array.prototype[Symbol.unscopables] 是一个空对象,仅包含上述所有值为 true 的属性名称。它是 原型是 null,因此 Object.prototype 属性(如 toString)不会意外地变得不可作用域,并且 with 语句中的 toString() 将继续在数组上调用。

¥Array.prototype[Symbol.unscopables] is an empty object only containing all the above property names with the value true. Its prototype is null, so Object.prototype properties like toString won't accidentally be made unscopable, and a toString() within the with statement will continue to be called on the array.

请参阅 Symbol.unscopables 了解如何为你自己的对象设置不可作用域的属性。

¥See Symbol.unscopables for how to set unscopable properties for your own objects.

示例

¥Examples

想象一下下面的 values.push('something') 调用是在 ECMAScript 2015 之前编写的代码。

¥Imagine the values.push('something') call below is in code that was written prior to ECMAScript 2015.

js
var values = [];

with (values) {
  values.push("something");
}

当 ECMAScript 2015 引入 Array.prototype.values() 方法时,上述代码中的 with 语句开始将 values 解释为 values.values 数组方法,而不是外部 values 变量。values.push('something') 调用将中断,因为它现在正在通过 values.values 方法访问 push。这导致向 Firefox(Firefox 错误 883914)报告了一个错误。

¥When ECMAScript 2015 introduced the Array.prototype.values() method, the with statement in the above code started to interpret values as the values.values array method instead of the external values variable. The values.push('something') call would break because it's now accessing push on the values.values method. This caused a bug to be reported to Firefox (Firefox Bug 883914).

因此,Array.prototype[Symbol.unscopables] 数据属性会导致 ECMAScript 2015 中引入的 Array 属性因 with 语句绑定目的而被忽略 — 允许 ECMAScript 2015 之前编写的代码继续按预期工作,而不是中断。

¥So the [Symbol.unscopables] data property for Array.prototype causes the Array properties introduced in ECMAScript 2015 to be ignored for with statement-binding purposes — allowing code that was written prior to ECMAScript 2015 to continue working as expected, rather than breaking.

规范

Specification
ECMAScript Language Specification
# sec-array.prototype-@@unscopables

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看