Skip to content

Instantly share code, notes, and snippets.

@schell
Created February 25, 2010 02:45
Show Gist options
  • Save schell/314168 to your computer and use it in GitHub Desktop.
Save schell/314168 to your computer and use it in GitHub Desktop.
/**
* Lists the (at least enumerable) properties of an object.
*
* @param Object the object to list
* @param Number the level to print at (num tabs)
* @param Boolean whether or not to print function code
* @author Schell Scivally
* @since Wed Feb 24 11:10:24 PST 2010
*/
this.dump = function(obj, lvl, pf) {
if(lvl == null)
lvl = 0;
if(pf == null)
pf = false;
var s = "";
//The padding given at the beginning of the line.
var tabs = " ";
var i = lvl;
while(i-- > 0)
tabs += " ";
if (obj.constructor.toString().indexOf("Array") == -1)
s += obj.toString() + "\n";
else
s += "[array]\n";
if( typeof(obj) == 'object') {
for(var item in obj) {
var value = obj[item];
if(typeof(value) == 'object') {
s += tabs + item + " => ";
s += this.dump(value, lvl + 1, pf);
} else {
s += tabs + item + " => " + this.dump(value, 0, pf) + "\n";
}
}
} else {
var type = typeof(obj);
if(type != 'function')
s = "["+ typeof(obj) + " '" + obj + "']";
else if(type == 'function' && pf)
s = "[function] : \n\n" + obj + "\n";
else
s = "[function]";
}
return s;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment