Skip to content

Instantly share code, notes, and snippets.

@jensarps
Created July 22, 2015 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jensarps/dc6f3d6c3933f183e873 to your computer and use it in GitHub Desktop.
Save jensarps/dc6f3d6c3933f183e873 to your computer and use it in GitHub Desktop.
A reflection function to (recursively) print object members onto the console
var _reflect = function reflect (it, recursive, level) {
level = ~~level;
var indent = (' >').repeat(level),
isObject = function (candidate) { return Object.prototype.toString.call(candidate) == '[object Object]'};
if (level > 1) {
console.log(indent + ' recursion too deep, stopping.');
return;
}
for (var key in it) {
// hasOwnProperty check intentionally omitted
console.log(indent + ' %c' + key +'%c %c(' + typeof it[key] + ')%c:', 'color:green', '', 'color:blue;font-style:italic', '', it[key]);
if (recursive && isObject(it[key])) {
reflect(it[key], true, level + 1);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment