It is possible using Object.defineProperty
Basically you redefine the set and get methods of the property you like to monitor with a code similar to the following:
Object.defineProperty(obj, propertyName, {
configurable: true,
enumerable: true,
set: function(val) {
notifyAll(val); // This is a custom function to notify
// the new value to all the listeners
value = val;
},
get: function() {
return value;
}
});
For example
var obj = {};
Object.defineProperty(obj, 'name', {
configurable: true,
enumerable: true,
set: function(val) {
console.log('Changed name to: ' + val);
value = val;
},
get: function() {
return value;
}
});
obj.name = 'pippo'; // Logs Changed name to pippo
obj.name = 'pluto'; // Logs changed name to pluto
console.log(obj.name); // Logs pluto