Last active
December 20, 2016 23:11
-
-
Save tedicela/438c99b83f1b1c0054f63e4219436c83 to your computer and use it in GitHub Desktop.
Debug function in Javascript like var_dump in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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