For any variable or property thereof, is there a way to know when its value is set?
For example, say I have:
let x = { 'a': 1, 'b': 2 };
// x.a's set operation is linked to a method
x.a = 3; // the method is automatically called
Is there a way I can call a function when a's value is changed? Lots of code would be changing this value; I don't want to add the method call all over the place.
I'm aware of Proxies, but to use them seems to require a separate variable. Meaning, x can't be a proxy of itself.
Preferably this technique would work with primitive and non-primitives.
Is there a way I can call a function when a's value is changed?use setters and gettersxcan be a Proxy....x = new Proxy(x, handler)x.anything since setting a value will callset: function(obj, prop, value) {... soobj[prop] = valuewon't loop :p butx[prop] = valuewould not be good