As Yoshi stated in a comment, it could be possible using Object.observe()
from the ES7 draft.
However it's not exactly a "catch-all setter" because it will only be triggered after the property changed, not before. So, if for example you want to store the property somewhere else, you will have to delete
it. Since the observe
callback is asynchronous, it will be ran after the current callstack, meaning the new value can be immediately used before being altered.
Also, Chrome only for now.
The following snippet does some manipulations on the object through native setting and using Object.observe
. It logs in the following order:
- I added this value: foobar
- The callback retrieves: foobar
- Value of foo.bar after deletion: undefined
Here goes:
var foo = {};
Object.observe(foo, function(changes) {
var lastChanges = changes[changes.length - 1],
newValue = lastChanges.object[lastChanges.name];
console.log('The callback retrieves: ' + newValue);
delete lastChanges.object[lastChanges.name];
}, ['add']);
foo.bar = 'foobar'; //Log n°2
console.log('I added this value: ' + foo.bar); //Log n°1
setTimeout(function() {
console.log('Value of foo.bar after deletion: ' + foo.bar); //Log n°3
}, 0); //Execute after the observe callback
Due to the fact that it's in the ES7 draft, the previous might be completely wrong depending on when you read this.