Skip to content

Instantly share code, notes, and snippets.

@tedicela
Last active December 20, 2016 23:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tedicela/438c99b83f1b1c0054f63e4219436c83 to your computer and use it in GitHub Desktop.
Save tedicela/438c99b83f1b1c0054f63e4219436c83 to your computer and use it in GitHub Desktop.
Debug function in Javascript like var_dump in PHP
/*
Debug function for Javascript like var_dump in PHP:
*/
function var_dump(obj, indent){
if(typeof indent == "undefined" ) indent="\n";
objType = typeof obj;
if(objType == "object" ){
str = "Object(";
if(Array.isArray(obj)) str = "Array(";
Object.keys(obj).forEach(function(key, index){
objIndent = indent+"\t";
str += objIndent+"["+key+"]=>"+debug(obj[key], objIndent);
});
str += indent+")";
}else{
if(typeof obj == "string" ) obj = "'"+obj+"'";
str = objType+"("+obj+")";
}
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment