Is it possible to listen to property changes without the use of Proxy
and setInterval
?
For common objects you could use the function below but that works for all existing properties but doesn't work for any properties that might get added after the wrapping.
function wrap(obj) {
var target = {};
Object.keys(obj).forEach(function(key) {
target[key] = obj[key];
Object.defineProperty(obj, key, {
get: function() {
console.log("Get");
return target[key];
},
set: function(newValue) {
console.log("Set");
target[key] = newValue;
}
});
});
}
var obj = {
a: 2,
b: 3
};
wrap(obj);
obj.a; // Get
obj.a = 2; // Set
obj.b; // Get
obj.b = 2; // Set
obj.c = 2; // Nothing
obj.c; // Nothing
If the object is an array you could also listen to the length
property and reset all the get
and set
functions when it's changed. This is obviously not very efficient as it changes the properties of each element whenever an element is added or removed.
So I don't think that Object.defineProperty
is the answer.
The reason I don't want to use setInterval
is because having big intervals will make the wrapping unreliable whereas having small intervals will have a big impact on the efficiency.