Skip to content

Instantly share code, notes, and snippets.

@archan937
Created March 2, 2012 22:16
Show Gist options
  • Save archan937/1961799 to your computer and use it in GitHub Desktop.
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.
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