Skip to content

Instantly share code, notes, and snippets.

@duobei
Last active December 10, 2015 16:59
Show Gist options
  • Save duobei/4464984 to your computer and use it in GitHub Desktop.
Save duobei/4464984 to your computer and use it in GitHub Desktop.
查看 Javascript 对象内容
// from http://www.nowamagic.net/librarys/veda/detail/802
// 简单版
function dump_obj(aObject) {
var s = "";
for (var property in aObject) {
s = s + "\n " + property + ": " + aObject[property];
}
alert(s);
}
var obj = {name:'Gonn', class:'NowaMagic.net'};
//alert(dump_obj(myObject));
// 复杂版
var MAX_DUMP_DEPTH = 10;
function dumpObj(obj, name, indent, depth)
{
if (depth > MAX_DUMP_DEPTH)
{
return indent + name + ": <Maximum Depth Reached>\n";
}
if (typeof obj == "object")
{
var child = null;
var output = indent + name + "\n";
indent += "\t";
for (var item in obj)
{
try
{
child = obj[item];
}
catch (e)
{
child = "<Unable to Evaluate>";
}
if (typeof child == "object") {
output += dumpObj(child, item, indent, depth + 1);
}
else {
output += indent + item + ": " + child + "\n";
}
}
return output;
}
else
{
return obj;
}
}
var myObject = {name: "Jack B. Nimble", 'goto': 'Jail', grade: 'A', level: 3};
//alert( dumpObj(myObject) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment