2010-07-30 44 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瀏覽器開發工具。你可以傳入任何對象來找出它裏面的內容。非常有幫助