使用 Proxy 创建动态不可配置的属性
Create dynamic non-configurable properties using Proxy
我想使用 Proxy 创建动态的不可配置属性。我试过这个:
const proxy = new Proxy({}, {
getOwnPropertyDescriptor() {
return {
configurable: false,
enumerable: false,
};
},
});
console.log(Reflect.getOwnPropertyDescriptor(proxy, 'test'));
但是我收到一个错误:
TypeError: 'getOwnPropertyDescriptor' on proxy: trap reported non-configurability for property 'test' which is either non-existant or configurable in the proxy target
MDN 表示:
A property cannot be reported as non-configurable, if it does not exists as an own property of the target object or if it exists as a configurable own property of the target object.
但这并没有解释这背后的原因。
是否有解决此错误的方法?
不是真的。这是由于理想的不变性,如果您在对象中观察到不可配置的 属性,它不会神奇地消失。如果它也是不可写的,它的值不能改变。
如果不能靠这个,getOwnPropertyDescriptor
基本没用
强制您不要使用不可配置的属性,或在目标中定义它们,意味着您不会违反此不变量,因为不变量通过构造保持在目标上。也就是说,ECMAScript 不允许您以破坏这些不变量的方式使用代理自定义。
Some of the internal method invariants defined in 6.1.7.3 are
essential integrity invariants. These invariants are explicitly
enforced by the proxy object internal methods specified in this
section. An ECMAScript implementation must be robust in the presence
of all possible invariant violations.
因此要么将 属性 报告为可配置,要么在目标中定义不可配置的属性。
如果你想要动态属性,我建议你撒谎说 属性 是可配置的。然后添加一个defineProperty
trap which returns false
,有效防止重定义
似乎如果在 getOwnPropertyDescriptor
陷阱中我在返回描述符之前在目标对象上定义 属性,它工作正常。
const proxy = new Proxy({}, {
getOwnPropertyDescriptor(target, property) {
const descriptor = {
configurable: false,
enumerable: false,
};
Reflect.defineProperty(target, property, descriptor);
return descriptor;
},
});
console.log(Reflect.getOwnPropertyDescriptor(proxy, 'test'));
副作用是(显然)创建的 属性,无法删除(因为它是不可配置的),这意味着例如我以后不能报告它不存在,但在我的无所谓。
我想使用 Proxy 创建动态的不可配置属性。我试过这个:
const proxy = new Proxy({}, {
getOwnPropertyDescriptor() {
return {
configurable: false,
enumerable: false,
};
},
});
console.log(Reflect.getOwnPropertyDescriptor(proxy, 'test'));
但是我收到一个错误:
TypeError: 'getOwnPropertyDescriptor' on proxy: trap reported non-configurability for property 'test' which is either non-existant or configurable in the proxy target
MDN 表示:
A property cannot be reported as non-configurable, if it does not exists as an own property of the target object or if it exists as a configurable own property of the target object.
但这并没有解释这背后的原因。
是否有解决此错误的方法?
不是真的。这是由于理想的不变性,如果您在对象中观察到不可配置的 属性,它不会神奇地消失。如果它也是不可写的,它的值不能改变。
如果不能靠这个,getOwnPropertyDescriptor
基本没用
强制您不要使用不可配置的属性,或在目标中定义它们,意味着您不会违反此不变量,因为不变量通过构造保持在目标上。也就是说,ECMAScript 不允许您以破坏这些不变量的方式使用代理自定义。
Some of the internal method invariants defined in 6.1.7.3 are essential integrity invariants. These invariants are explicitly enforced by the proxy object internal methods specified in this section. An ECMAScript implementation must be robust in the presence of all possible invariant violations.
因此要么将 属性 报告为可配置,要么在目标中定义不可配置的属性。
如果你想要动态属性,我建议你撒谎说 属性 是可配置的。然后添加一个defineProperty
trap which returns false
,有效防止重定义
似乎如果在 getOwnPropertyDescriptor
陷阱中我在返回描述符之前在目标对象上定义 属性,它工作正常。
const proxy = new Proxy({}, {
getOwnPropertyDescriptor(target, property) {
const descriptor = {
configurable: false,
enumerable: false,
};
Reflect.defineProperty(target, property, descriptor);
return descriptor;
},
});
console.log(Reflect.getOwnPropertyDescriptor(proxy, 'test'));
副作用是(显然)创建的 属性,无法删除(因为它是不可配置的),这意味着例如我以后不能报告它不存在,但在我的无所谓。