Skip to content

Instantly share code, notes, and snippets.

@davidmfoley
Forked from re5et/gist:3036720
Created July 3, 2012 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save davidmfoley/3041435 to your computer and use it in GitHub Desktop.
Save davidmfoley/3041435 to your computer and use it in GitHub Desktop.
JavaScript Window global var differ.
var Differ = function(thing){
this.thing = thing;
};
Differ.prototype.cacheVars = function(){
this.cache = {};
for(var prop in this.thing){
this.cache[prop] = this.thing[prop];
}
};
Differ.prototype.diff = function(){
for(var prop in window){
// it is the same
if(this.thing[prop] === this.cache[prop]){
continue;
}
else{
// it has changed
if(this.cache[prop] && this.thing[prop] !== this.cache[prop]){
console.log(prop+' changed from: '+this.cache[prop]+' to: '+this.thing[prop] + '!');
}
else{
// it is new
if(this.thing[prop] && !this.cache[prop]){
console.log(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 && this.thing[prop] === undefined){
console.log(prop+' was deleted!');
}
}
};
// Use like:
//
var vars = new Differ(window);
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