Jonathan Szekely
Jonathan Szekely

Reputation: 207

How can i extend a javascript object?

I have 2 js files which should handle the configuration of my app. In the first i have the general config and in the second a more specific config. The second file is supposed to extend the first but when i try to access the object from it it says it's undefined. Here be code:

1ST file:

var Config = {

    application.name = ""

};

2ND file:

Config.menus.mainMenu = [

    {
        "route": "/address-validation",
        "prefix": "address",
        "label": "Address Validation",
        "icon": "icon-8"
    }, 
    {
        "route": "/reports/onhand",
        "prefix": "reports",
        "label": "Reports",
        "icon": "icon-9"
    }, 
    {
        "route": "/performance/sales",
        "prefix": "performance",
        "label": "Performance",
        "icon": "icon-10"
    }, 
    {
        "route": "/taxes/nonhumanminnesota",
        "prefix": "taxes",
        "label": "Taxes",
        "icon": "icon-11"
    }, 
    {
        "route": "/utils/product-location-mass-assignation",
        "prefix": "util",
        "label": "Utils",
        "icon": "icon-12"
    }, 
    {
        "route": "/administration",
        "prefix": "administration",
        "label": "Administration",
        "icon": "icon-7"
    }
];

Upvotes: 0

Views: 42

Answers (1)

Cerbrus
Cerbrus

Reputation: 72857

Config.menus is undefined, so you can't set Config.menus.mainMenu on it.

Try this in your second file:

Config.menus = Config.menus || {}; // Set `Config.menus` to an empty object if it's undefined
Config.menus.mainMenu = [          // The rest of your code here

Upvotes: 1

Related Questions