Created
July 22, 2015 12:10
-
-
Save jensarps/dc6f3d6c3933f183e873 to your computer and use it in GitHub Desktop.
A reflection function to (recursively) print object members onto the console
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
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