Created
July 3, 2012 01:00
-
-
Save re5et/3036720 to your computer and use it in GitHub Desktop.
JavaScript Window global var differ.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var GlobalVars = function(){}; | |
GlobalVars.prototype.cacheVars = function(){ | |
this.cache = {}; | |
for(var prop in window){ | |
this.cache[prop] = window[prop]; | |
} | |
}; | |
GlobalVars.prototype.diff = function(){ | |
for(var prop in window){ | |
// it is the same | |
if(window[prop] === this.cache[prop]){ | |
continue; | |
} | |
else{ | |
// it has changed | |
if(this.cache[prop] && window[prop] !== this.cache[prop]){ | |
console.log('window.'+prop+' changed from: '+this.cache[prop]+' to: '+window[prop] + '!'); | |
} | |
else{ | |
// it is new | |
if(window[prop] && !this.cache[prop]){ | |
console.log('window.'+prop+' was introduced! Value is: '+window[prop]); | |
} | |
} | |
} | |
} | |
// check for delete properties | |
for(var prop in this.cache){ | |
// it was deleted | |
if(this.cache[prop] !== undefined && window[prop] === undefined){ | |
console.log('window.'+prop+' was deleted!'); | |
} | |
} | |
}; | |
// Use like: | |
// | |
var vars = new GlobalVars(); | |
window.foo = 'bar' | |
window.bar = 'baz' | |
vars.cacheVars(); | |
window.qux = 1; | |
window.foo = function(){console.log('baz')}; | |
delete window.bar; | |
vars.diff(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment