Afaik, .watch() is just SpiderMonkey (Firefox).
I played with the polling function, I finally came up with this:
var mywatch = (function() { var last = { count: 0, elems: {} }; return function _REP(cb) { var curr = { count: 0, elems: {} }, diff = {}; for(var prop in window) { if( window.hasOwnProperty(prop) ) { curr.elems[prop] = window[prop]; curr.count++; } } if( curr.count > last.count ) { for(var comp in curr.elems) { if( !(comp in last.elems) ) { diff[comp] = curr.elems[comp]; } } last.count = curr.count; last.elems = curr.elems; if(typeof cb === 'function') cb.apply(null, [diff]); } setTimeout(function() { _REP(cb); }, 400); }; }());
And then use it like:
mywatch(function(diff) { console.log('NEW GLOBAL(s): ', diff); });
Keep in mind that only new global variables are used for this. But you can easily expand this for the case last.count > curr.count . This indicates that global variables have been deleted.
source share