我如何扩展javascript对象?

时间:2015-06-18 10:18:50

标签: javascript

我有2个js文件,应该处理我的应用程序的配置。在第一个我有一般配置,在第二个更具体的配置。第二个文件应该扩展第一个,但是当我尝试从它访问对象时,它说它未定义。这是代码:

1ST档案:

var Config = {

    application.name = ""

};

2ND档案:

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"
    }
];

1 个答案:

答案 0 :(得分:1)

Config.menusundefined,因此您无法在其上设置Config.menus.mainMenu

在第二个文件中尝试此操作:

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