1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
function odump(object, depth, max){ depth = depth || 0; max = max || 2; if (depth > max) return false; var indent = ""; for (var i = 0; i < depth; i++) indent += " "; var output = ""; for (var key in object){ output += "\n" + indent + key + ": "; switch (typeof object[key]){ case "object": output += odump(object[key], depth + 1, max); break; case "function": output += "function"; break; default: output += object[key]; break; } } return output; }
Refactorings
No refactoring yet !
Andre Steenveld
February 3, 2008, February 03, 2008 19:18, permalink
It is pretty bad practice to extend the object prototype and the native types, but it comes in handy here and there. Made a little revision and made sure you end up walking around in circles.
code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
Object.prototype.dump = function( excludePrototype, maxDepth, depth ){ depth = depth || []; maxDepth = maxDepth >= 0 ? maxDepth : -1; excludePrototype = !!excludePrototype; // some working vars var output = [ "" ], key = "", indent = ""; for( var i = 0; i < depth.length; i++ ){ indent += " ";} if( maxDepth === -1 || depth.length < maxDepth ){ for( key in this ) if( ( this.hasOwnProperty( key ) || excludePrototype ) ){ if( depth.contains( this[ key ] ) ){ output[ 0 ] = "{circular reference}"; } else { depth.push( this[ key ] ); output.push( indent + key + ": " + this[ key ].dump( excludePrototype, maxDepth, depth ) ); depth.pop( this[ key ] ); } } } else { output[ 0 ] = "{object}"; } return output.join("\n"); }; Array.prototype.contains = function( obj ){ for( var i = 0; i < this.length; i++ ){ if( this[ i ] === obj ){ return true; } } return false; }; // default dumpster method var defaultDumpster = function(){ return this.toString(); }; String.prototype.dump = Number.prototype.dump = RegExp.prototype.dump = Boolean.prototype.dump = defaultDumpster; Function.prototype.dump = function(){ return "{function}"; }; Date.prototype.dump = function(){ return "{" + this.getTime() + "}"; };
example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
var obj = { num: 123, str: "testString", bln: true, arr: [ 1, 2, 3 ], fnc: function(){ return "moooo"; }, rxp: /^.*$/gi, dt: new Date(), obj: { num: 123, str: "testString", bln: true, arr: [ 1, 2, 3 ], fnc: function(){ return "moooo"; }, rxp: /^.*$/gi } } obj.obj.obj = obj; // Doesn't include the methods/properties declared in a objects prototype. obj.dump( false ) // Iterate throught the first object and also dump evrything (not native) of the prototype. obj.dump( true, 1 );
Backwards compatability
1 2 3 4
function odump(obj, depth, max){ return obj.dump( true, max ); }
Tien Dung
July 25, 2008, July 25, 2008 10:00, permalink
More JavaScript magic and easier to read :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
var odump = (function(){ var max, INDENT = " "; // As long as you need :) function valueToStr(value, depth) { switch (typeof value) { case "object": return objectToStr(value, depth + 1); case "function": return "function"; default: return value; } } function objectToStr(object, depth) { if (depth > max) return false; var output = ""; for (var key in object) output += "\n" + INDENT.substr(0,2*depth) + key + ": " + valueToStr(object[key], depth); return output; }; return function odump(object, depth, _max) { max = _max || 2; return objectToStr(object, depth || 0); }; })();
Given an object, iterate through it and dump the properties.