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); }; })();
mud
June 23, 2009, June 23, 2009 21:55, permalink
I cleaned it up so it spits out proper objects that you can copy and paste back into your javascript. Eh, the code is messier now, because I added a few new features as well:
(1) when you run TEST() the select argument (if true) will aggregate the results. This makes it easier to do a TEST inside of a for loop (without creating a temp variable, ect... just do TEST("text", true) inside the loop.
(2) it automatically creates a text area to spit the data out to (if it's already been created, uses that one). this just makes it easier to get started on new projects. All you have to do is include this javascript, and then type: TEST(whatever); and it will spit it out to the screen.
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
TEST = { }; TEST = (function() { var max, depth = 0, INDENT = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t"; function valueToStr(value, depth) { switch(typeof value) { case "object": return objectToStr(value, depth + 1); case "function": return "function"; case "string": return "'"+value+"'"; default: return value; } }; function objectToStr(object, depth) { if(depth > max) return false; var type = Object.prototype.toString.call(object), output = "\n", indent = INDENT.substr(0, depth); for(var key in object) output += indent + valueToStr(key) + ": " + valueToStr(object[key], depth) + ",\n"; indent = INDENT.substr(0, depth - 1); switch(type) { case "[object Object]": return "{ " + output.substr(0, output.length - 2) + "\n" + indent + "}"; case "[object Array]": return "[ " + output.substr(0, output.length - 2) + "\n" + indent + "]"; default: return; } }; return function(value, aggregate, MAX) { max = MAX || 2; value = valueToStr(value, depth); var d = document.getElementById("TEST"); if(!d) { // TEST does not exist var d = document.createElement("textarea"); d.id = "TEST"; document.body.appendChild(d); } d.innerHTML = aggregate ? d.innerHTML + value : value; }; })();
Given an object, iterate through it and dump the properties.