簡體   English   中英

如何擴展JavaScript對象?

[英]How can i extend a javascript object?

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM