In javascript, I can detect when a global variable foo is set (assuming that it is initially undefined) by using Object.defineProperty:
var foo_;
Object.defineProperty(window, 'foo', {
get: function() {
return foo_;
},
set: function(newFoo) {
console.log('foo set to ' + newFoo);
foo_ = newFoo;
}
});
Is there a more elegant way to do this? One downside is that I cannot call Object.defineProperty on the same property twice. Overriding the entire property just to detect when it is defined seems a bit overkill.
Could I somehow use proxies? I feel like I would have to make a proxy that targets the window object though ... is that efficient?
window[foo]PromiseObject.definePropertyon that property.