2012-03-09 40 views
3

修訂版從原來的問題JavaScript對象調查

我想學什麼JavaScript對象「看起來像」通過觀察它,像這樣的屬性:

//create object without prototype 
var obj = Object.create(null); 
//add properties 
obj.a = 3; 
obj.b = 76.3; 
obj.c = { a : 23 , b : true , c : 1}; 
obj.s = "ABC!"; 
//output string 
var outStr = new String(); 

for(var prop in obj) 
{ 
    outStr += "Property " + prop + " is a " + typeof(obj[prop]) + " with value " + obj[prop] + "\n"; 

    for(var prop1 in obj[prop]) 
    { 
     outStr += "Property " + prop1 + " is a " + typeof(obj[prop][prop1]) + " with value " + obj[prop][prop1] + "\n"; 
     for(var prop2 in obj[prop][prop1]) 
     { 
      outStr += "Property " + prop2 + " is a " + typeof(obj[prop][prop1][prop2]) + " with value " + obj[prop][prop1][prop2] + "\n"; 
      for(var prop3 in obj[prop][prop1][prop2]) 
      { 
       outStr += "Property " + prop3 + " is a " + typeof(obj[prop][prop1][prop2][prop3]) + " with value " + obj[prop][prop1][prop2][prop3] + "\n"; 
       for(var prop4 in obj[prop][prop1][prop2][prop3]) 
       { 
        outStr += "Property " + prop4 + " is a " + typeof(obj[prop][prop1][prop2][prop3][prop4]) + " with value " + obj[prop][prop1][prop2][prop3][prop4] + "\n"; 
       } 
      } 
     } 
    } 
} 

alert(outStr); 

其中給出輸出:

Property a is a number with value 3 
Property b is a number with value 76.3 
Property c is a object with value [object Object] 
    Property a is a number with value 23 
    Property b is a boolean with value true 
    Property c is a number with value 1 
Property s is a string with value ABC! 
    Property 0 is a string with value A 
    Property 0 is a string with value A 
    Property 0 is a string with value A 
    Property 0 is a string with value A 
    Property 1 is a string with value B 
    Property 0 is a string with value B 
    Property 0 is a string with value B 
    Property 0 is a string with value B 
    Property 2 is a string with value C 
    Property 0 is a string with value C 
    Property 0 is a string with value C 
    Property 0 is a string with value C 
    Property 3 is a string with value ! 
    Property 0 is a string with value ! 
    Property 0 is a string with value ! 
    Property 0 is a string with value ! 

現在,它的行爲與我期望的除了String屬性之外的每個屬性完全相同obj.s =「ABC!」;

我明白obj.s包含屬性(鍵和值):

「0」= 「A」

「1」= 「B」

「2」=「 C「

」3「=」!「

從前面的回答(非常感謝@pimvdb和@deestan)我收集到,因爲每個屬性值都是字符串,它們每個都包含屬性鍵「0」,它本身必須包含屬性鍵「0」等,等?這就是爲什麼我要爲字符串屬性寫出額外的行。

所以我的問題現在變成:

應的所有屬性的值,在某些點的類型,最終恢復到原始類型像數或布爾停止這種遞歸鏈?我正在考慮實際上持有這個對象的內存,當我開始寫這個小測試腳本來「查看」對象時,我基本上想看到所有的基元以及對象如何存儲它們,但實際上並不存在無限的字符串分配到字符串到字符串...我想它只是存儲爲字符串對象與它的4個字符(或5如果theres字符串字符的結尾太0)

回答

3

也許不是張貼一個問題的答案,但我認爲這是一個答案,你的實際的問題::)

安裝並公開Chrome的JavaScript控制檯(Ctrl + Shift + J)。在那裏你可以創建對象並從Javascript調試器直觀地檢查它們。

> x = "stringystringstring" 
    "stringystringstring" 
> 

然後在右上角,加x到「監視表達式」,以檢查它。

正如你所看到的,x作爲一個字符串是不是很有意思,所以讓我們把它更加複雜:

> x = { a: "b" } 
|> Object 
> 

刷新監視表達式和擴大x看到物體的結構。它應該是這個樣子:

v x: Object 
    a: "b" 
    v __proto__: Object 
    |> __defineGetter__: function __defineGetter__()... 
    |> __defineSetter__: function __defineSetter__()... 
    ... 
    |> valueOf: function valueOf() { [native code] } 

我們從中學到了什麼是對象x有一個屬性,和一堆方法繼承從對象的原型。


如果您需要看到所有屬性和方法訪問例如字符串,在控制檯中鍵入"derp".。半秒後,應顯示一個顯示所有可用屬性的自動完成列表。

+0

謝謝你,這是非常有幫助的答案我一定會從現在開始使用JavaScript控制檯。 – 0xor1 2012-03-09 22:19:25

2

你迭代的鍵首先是"ABC!"。對於每個屬性(字母),您都在迭代鍵(單字母字符串有一個屬性),即"0"(鍵總是字符串)。該字符串包含一個屬性,其中包含鍵"0"(因爲它是第一個也是唯一的字符)和值"0"(上一級的鍵)。你在這個屬性上做的事情完全一樣,所以你最終會得到一個string 0 0'遞歸'模式。

我不知道爲什麼您預計會在2個水平停止,因爲字符串"A"仍然可以檢查一樣"ABC!" - 事實上,如果我理解正確,你的方法永遠不會停止。

+0

謝謝,這真的有幫助,我已經改變了循環的價值而不是關鍵,它仍然產生同樣的問題,因爲該值也是字符串類型,所以我最終出現了一個無限數量的字符串「0」指向原始字符串中的每個字符:S – 0xor1 2012-03-09 22:19:18

+0

@Danny R:使用該值而不是鍵將不會真正幫助我猜測,因爲'「ABC!」'會產生'「A」', (「ABC」,「contains」,「A」,其中包含「A」,...)。當然,你可以停止使用單字母字符串,但這不是一個令人滿意的方法。 – pimvdb 2012-03-09 22:28:05