I would like to know the correct way to create a nested object in javascript. I want a base object called “defaultsettings”. It should have 2 properties (object type): ajaxsettings and uisettings. I know that i can write something like
var defaultsettings = new Object();
var ajaxsettings = new Object();
defaultsettings.ajaxsettings = ajaxsettings.. etc.
But what i want to know is how to type it this way (that i suppose is a more correct way of doing it):
var defaultsettings = {
var ajaxsettings = { ... }
};
I suppose you get the idea. Thanks!
If you know the settings in advance you can define it in a single statement:
If you don’t know the values in advance you can just define the top level object and then add properties:
Or half-way between the two, define the top level with nested empty objects as properties and then add properties to those nested objects:
You can nest as deep as you like using the above techniques, and anywhere that you have a string literal in the square brackets you can use a variable:
Note that you can not use variables for key names in the { } literal syntax.