我想使用代理创建动态的不可配置属性。我尝试这样做:使用代理创建动态的不可配置属性
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.
但它并不能说明什么这背后的推理。
有没有解决此错误的方法?
很好的解释,谢谢。你如何看待我的解决方案(发布为自己的答案)?是否有任何我没有想到的副作用? –
@Gothdo是的,这应该正常工作。你可能会遇到像另一个代理或不可扩展对象那样的更一般的目标,但如果你使用'{}'而不把它暴露给其他代码,我没有看到任何问题。 – Oriol