Skip to content

Instantly share code, notes, and snippets.

@gotascii
Created January 21, 2011 13:57
Show Gist options
  • Save gotascii/789705 to your computer and use it in GitHub Desktop.
Save gotascii/789705 to your computer and use it in GitHub Desktop.
Object.prototype.inspect = function () {
function pad(depth) {
return Array(depth).join(" ");
}
function down(attr, val, depth) {
var output = pad(depth) + attr + ': ';
output += (typeof(val) == 'object' ? "{" : val);
return output + "\n";
}
function up(depth) {
var output = pad(depth) + "}";
if (depth > 1) { output += "\n"; }
return output;
}
function traverseObject(obj, depth) {
var output = "";
var depth = depth || 1;
Object.keys(obj).forEach(function (attr) {
var val = obj[attr];
output += down(attr, val, depth);
if (typeof(val) == 'object') {
output += traverseObject(val, depth + 1) + up(depth);
}
});
return output;
}
return traverseObject(this);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment