I found this rather odd, consider this snippet:
const data = {};
const prox = new Proxy(data, {
get(target, key) {
console.log("proxy get triggered", key);
return target[key];
},
set(target, key, value) {
console.log("proxy set triggered", key, value);
return target[key] = value;
}
});
Object.defineProperty(prox, "my_prop", {
get() {
console.log("defineProperty get triggered");
return window.my_prop;
},
set(v) {
console.log("defineProperty.set triggered", v);
return window.my_prop = v;
}
});
console.log("setting prox.my_prop = 5");
prox.my_prop = 5;
console.log("set complete");
console.log("window.my_prop", window.my_prop);
console.log("data.my_prop", data.my_prop);
The result is confusing the hell out of me. Basically, it seems that by trying to define a property on the proxy, it instead defines it on the target of the proxy? Otherwise I don't see how it can trigger both setters and the value to end up where defineProperty wants to put it.