Created
March 2, 2012 22:16
-
-
Save archan937/1961799 to your computer and use it in GitHub Desktop.
Inspect Javascript objects similar to 'object.toSource()' within Mozilla. Used in IE6+, Safari, Firefox and Chrome.
This file contains hidden or 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
function inspect(object) { | |
switch (typeof(object)) { | |
case "undefined": | |
return "undefined"; | |
case "string": | |
return "\"" + object.replace(/\n/g, "\\n").replace(/\"/g, "\\\"") + "\""; | |
case "object": | |
if (object == null) { | |
return "null"; | |
} | |
var a = []; | |
if (object instanceof Array) { | |
for (var i in object) { | |
a.push(inspect(object[i])); | |
}; | |
return "[" + a.join(", ") + "]"; | |
} else { | |
for (var key in object) { | |
if (object.hasOwnProperty(key)) { | |
a.push(key + ": " + inspect(object[key])); | |
} | |
}; | |
return "{" + a.join(", ") + "}"; | |
} | |
default: | |
return object.toString(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment