Search code examples
javascriptooppropertiesnestedobject-literal

How far can an object literal be nested?


I have found that it is possible to nest another property within an object literal property. Here is an example of this:

var rectangle = { 
    upperLeft : { x : 2, y : 2 },
    lowerRight : { x : 4, y : 4}
};

However, when I tried to nest yet another property within that nested property, I could not get this to work. Is maybe my syntax incorrect? Here is my code:

var rectangle = { 
    upperLeft : { x : {min: 2, max: 4}, y : 2 },
    lowerRight : { x : {min: 4, max: 6}, y : 4}
};

Solution

  • Your second sample is fine. When you access:

    rectangle.upperLeft.x.min
    

    it correctly returns 2. What problems do you encounter? Note that there is no theoretical maximum nesting level.