2013-03-26 68 views
0

當試圖通過數據對象的值枚舉,其給出了其所有屬性的未定義值,但越來越不枚舉值給出字符串數據(工作)的值...爲什麼通過一個對象枚舉給定一個undefine?

創建jstree節點,而沒有問題...

$("#treeFile1").jstree("create", null, "outside", { "attr" : { "rel" : "folder" }}); 

偵聽該事件的上述

$("#treeFile1").bind("create.jstree", function(event,data) 
{  
alert(data.args.toSource()); // gives a string output not defined 
    alert(data.inst.toSource()); 

for(var prop in data) 
{ 
    alert("Property name is: "+ prop + " property value is: "+ data.prop); 
     // gives each value as undefined, why is this? 
} 
    event.stopImmediatePropagation(); 

回答

4

因爲你需要使用bracket notation,而不是dot notation使用prop作爲訪問屬性名稱:

MDN的文檔提供了a solid for...in example

+1

1;花式ES5方式:'Object.keys(數據).forEach(函數(P){警報(數據[P])});'保存針對該對象的枚舉的屬性的檢查。 – Christoph 2013-03-26 16:19:21

+0

確實如此,但當然這在IE不斷老舊的版本中並不支持。 – 2013-03-26 16:22:16

+0

沒錯,但是IE9「已經」能夠做到這一點。所以它不會太遠,因爲我們可以毫無問題地使用它。 – Christoph 2013-03-26 16:27:02

相關問題