根据MDN,
在这种情况下,如何同时监视和允许继承对象上的本地赋值?
handler.set()
可以捕获继承属性分配:Object.create(proxy)[foo] = bar;
在这种情况下,如何同时监视和允许继承对象上的本地赋值?
var base = {
foo: function(){
return "foo";
}
}
var proxy = new Proxy(base, {
set: function(target, property, value, receiver){
console.log("called: " + property + " = " + value, "on", receiver);
//receiver[property] = value; //Infinite loop!?!?!?!?!
//target[property] = value // This is incorrect -> it will set the property on base.
/*
Fill in code here.
*/
return true;
}
})
var inherited = {}
Object.setPrototypeOf(inherited, Object.create(proxy));
inherited.bar = function(){
return "bar";
}
//Test cases
console.log(base.foo); //function foo
console.log(base.bar); //undefined
console.log(inherited.hasOwnProperty("bar")) //true
writable: true, enumerable: true, configurable: true
,使其等价。 - T.J. Crowder