Last updated on
Symbol.unscopables
Symbol.unscopables 是 ES6 引入的一个特殊的内置符号,用于指定在 with 语句块中哪些属性是不可见的。换句话说,它定义了对象中在 with 语句作用域中被排除的属性。
const obj = {
foo: 1,
bar: 2,
[Symbol.unscopables]: {
foo: true, // foo 属性在 with 语句中不可见
},
};
with (obj) {
console.log(foo); // ReferenceError: foo is not defined
console.log(bar); // 2
}