let obj = new Proxy({},
{
get(target, key, receiver) {
console.log(`get ${key}`);
return Reflect.get(target, key, receiver)
},
set(target, key, value, receiver) {
console.log(`set ${key}`);
return Reflect.set(target, key, value, receiver);
}
});
obj.count = 1;
console.log(obj.count);
var person = {
name: "张三"
};
var proxy = new Proxy(person, {
get: function(target, property) {
if (property in target) {
return target[property];
} else {
throw new ReferenceError("Property \"" + property + "\" does not exist.");
}
}
});
proxy.name // "张三"
proxy.age // 抛出一个错误
console