2010-07-30 43 views

回答

10

为Firefox或开发工具与谷歌的Chrome/Safari浏览器来检查你的对象使用Firebug。这是我认为最好的方式。

+1

+1至点 – naikus 2010-07-30 07:41:12

1

考虑以下对象:

var x = { 
    property1: 'value1', 
    property2: 'value2', 
    property3: 'value3', 
    method1: function() { 
    return 0; 
    }, 
    method2: function() { 
    return 0; 
    } 
}; 

然后做:

for (var prop in x) { 
    console.log(prop); 
} 

输出:

property1 
property2 
property3 
method1 
method2 

您可能需要使用hasOwnProperty()方法,以确保你没有得到物体原型链的属性:

for (var prop in x) { 
    if (x.hasOwnProperty(prop)) { 
    console.log(prop); 
    } 
} 
1

使用该自定义函数或JSON.stringify(obj);

/** 
    * Gets the string representation of the specified object. This method is 
    * used for debugging 
    * @param {Object} Object to convert to string 
    * @return {String} The string representation of the object 
    */ 
    var toObjectSource = function(obj) { 
    if(obj === null) { 
     return "[null]"; 
    } 
    if(obj === undefined) { 
     return "[undefined]"; 
    } 

    var str = "["; 
    var member = null; 
    for(var each in obj) { 
     try { 
      member = obj[each]; 
      str += each + "=" + member + ", "; 
     }catch(err) { 
      alert(err); 
     } 
    } 
    return str + "]"; 
    } 
2

JavaScript对象是键/值对esentially地图。您可以通过点符号(例如myObject.someProp)或甚至通过索引符号(myObject["someProp"])访问成员。使用后者可能会帮助你:

function print(obj) { 
    for (var i in obj) 
     console.log(i + " - " + obj[i]); 
    } 
} 

运行通过Firebug的,看看你会得到:)

0

安装FireBug什么。它的插件为Mozilla Firefox

在您的源文件中,编写:console.log(yourObjectGoesHere);并转到FireBug中的“控制台”选项卡...以易于掌握的树形格式显示杀手对象发现。

3

如果您使用的是Mozilla Firefox,则可以使用Firebug。要查看您的变量或对象是什么,不喜欢它下面的代码(例如,我使用JSON变量)

var yourObject = {test: 'this is just a test'}; 
console.log(yourObject); 

您安装Firebug的,运行一个包含该JavaScript的HTML文件,并选择控制台选项卡中的萤火虫。你会在那里看到结果。希望这可以帮助你。 :)我

0

绝招:

console.dir(yourobject); 

看到它住在你的Chrome浏览器开发工具。你可以传入任何对象来找出它里面的内容。非常有帮助