I am using the Javascript Proxy system to allow individual functions to subscribe to any changes in an object defined by a class. For example purposes, say this is my code:
class Data {
i = 0
increment() {
this.i++;
}
}
const obj = new Data();
const objProxy = new Proxy(obj {
get: function(target, prop) {
return Reflect.get(target, prop, objProxy);
},
set: function(target, prop, value) {
console.log("setting", prop, "of", target);
return Reflect.set(target, prop, value, objProxy);
}
}
objProxy.increment();
The issue is that the this
reference in the increment
function is a reference to the object itself, not the proxy, so the proxy setter doesn’t seem to be triggered.
I tried to specify the object proxy as the “receiver” option for both the getter and the setter, expecting that the “this” reference would be changed to the proxy and the log message would be triggered when “i” is changed.