Very simple proxy using the set trap
suggest changeThis proxy appends the string " went through proxy" to every string property set on the target object. A value that is not a string is not stored. The trap returns undefined, so the assignment throws TypeError in strict mode after the string has been written.
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);
// { example: "ExampleValue went through proxy" }
// The target is object. proxied.target is not the target.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents