Created
June 15, 2011 18:14
-
-
Save cowboy/1027709 to your computer and use it in GitHub Desktop.
JavaScript: What's New? List properties that have been added to an object between a starting and ending point.
This file contains 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
// What's New? - v0.1.0 - 6/15/2011 | |
// http://benalman.com/ | |
// Copyright (c) 2011 Ben Alman; Licensed MIT, GPL | |
var whatsnew = (function() { | |
// Stuff. | |
var cache = []; | |
// Return a new object that has all the properties of the original object, but | |
// set to true. | |
function getprops(obj) { | |
var props = {}; | |
for ( var prop in obj ) { | |
if ( obj.hasOwnProperty(prop) ) { | |
props[prop] = true; | |
} | |
} | |
return props; | |
} | |
return { | |
// newprops.start('window', window); // initialize the cache. | |
start: function(name, obj) { | |
cache[name] = {obj: obj, props: getprops(obj)}; | |
}, | |
// newprops.end('window'); // see what props have been added and log. | |
end: function(name) { | |
var c = cache[name]; | |
var current = getprops(c.obj); | |
var result = []; | |
for ( var prop in current ) { | |
current.hasOwnProperty(prop) && !c.props[prop] && result.push(prop); | |
} | |
delete cache[name]; | |
console.log(result.length + ' properties introduced in ' + name + ': %o', result); | |
} | |
}; | |
}()); |
This file contains 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
<!DOCTYPE HTML> | |
<html lang="en-US"> | |
<head> | |
<meta http-equiv="content-type" content="text/html; charset=utf-8"> | |
<title>something-scary.js test</title> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script> | |
<script src="https://raw.github.com/gist/1027709/ba-whatsnew.js"></script> | |
<script> | |
// initialize | |
whatsnew.start('window', window); | |
whatsnew.start('$', $); | |
whatsnew.start('$.fn', $.fn); | |
</script> | |
<script src="something-scary.js"></script> | |
<script> | |
// log to console | |
whatsnew.end('window'); | |
whatsnew.end('$'); | |
whatsnew.end('$.fn'); | |
</script> | |
</head> | |
<body> | |
... | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment