非常简单的代理(使用 set trap)
此代理只是将字符串 went through proxy
附加到目标 object
上设置的每个字符串属性。
let object = {};
let handler = {
set(target, prop, value){ // Note that ES6 object syntax is used
if('string' === typeof value){
target[prop] = value + " went through proxy";
}
}
};
let proxied = new Proxy(object, handler);
proxied.example = "ExampleValue";
console.log(object);
// logs: { example: "ExampleValue went trough proxy" }
// you could also access the object via proxied.target