Exploring a Javascript Object

UPDATED FROM ORIGINAL QUESTION

I am trying to find out what a javascript object looks like by looking at it like this:

//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);

      

Which gives the result:

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 !

      

This now behaves exactly as I expect for every property except for the String property obj.s = "ABC!";

I understand that obj.s contains properties (keys and values):

"0" = "A"

"1" = "B"

"2" = "C"

"3" = "!"

And from the previous answer (thank you very much @pimvdb and @deestan), I understand that since each of these property values ​​are strings, they also contain the property key "0", which itself must also contain the property key "0" and etc.? so I am getting additional lines written out for the string property.

So now my question is:

Should the value type of all properties eventually fall back to a primitive type of type number or boolean at some point to stop this recursive chaining? I am thinking about the memory that actually stores this object, and when I started writing this little test script to "look" at the object, I basically wanted to see all the primitives and how the object stores them, but really not could be an infinite string-to-string allocation ... I am guessing it is just stored as a string object with 4 characters in it (or 5 if also the end of the string \ 0 character)

+3


source to share


2 answers


Perhaps not the answer to the question posed, but I believe this is the answer to your actual problem: :)

Install Chrome and open the Javascript Console (CTRL + SHIFT + J). There you can create objects and visually inspect them with the Javascript debugger.

> x = "stringystringstring"
  "stringystringstring"
>

      

Then in the upper right corner add x

to "Watch Expressions" to check it out.

As you can see, x

the string isn't very interesting, so make it more complex:

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

      



Update the Watch expressions and expand x

to see the object structure. It should look something like this:

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

      

From this we learn that the object x

has one property and many methods inherited from the Object prototype.


If you need to see all the properties and methods available, such as strings, enter "derp".

in the console. After half a second, an autocomplete list appears showing all available properties.

+3


source


First you iterate over the keys "ABC!"

. For each property (letter), you repeat the key (there is one property for a single letter string) which is "0"

(keys are always strings). This line contains one property that has a key "0"

(because it is the first and only character) and a value "0"

(the key of the previous level). You do the same on this property, so you get the string 0 0

'recursion' pattern .



I'm not sure why you expect to stop after two levels, because the string "A"

can be checked in exactly the same way "ABC!"

- in fact, your method will never stop if I understand you correctly.

+2


source







All Articles