const target = {};
Object.defineProperty(target,'foo',{
configurable:false,
writable:false,
value:'bar'
});
const handler = {
get(){
return 'qux';
}
}
const proxy = new Proxy(target,handler);
console.log(proxy.foo);//'get' on proxy: property 'foo' is a read-only and non-configurable data property on the proxy target but the proxy did not return its actual value (expected 'bar' but got 'qux')
- 捕获不变式是一种协议,用来规范捕获处理程序的行为。
- 捕获不变式因方法不同而异,但通常都会防止捕获器定义出现过于反常的行为。
- 上例中,目标对象的属性为不可写不可编辑,所以当捕获器返回一个与该属性不同的值的时候,会抛出TypeError错误。