gpt4 book ai didi

javascript - 我怎样才能扩展一个javascript对象?

转载 作者:行者123 更新时间:2023-12-03 09:49:37 26 4
gpt4 key购买 nike

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

第一个文件:

var Config = {

application.name = ""

};

第二个文件:

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.menus未定义,因此您无法对其设置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

关于javascript - 我怎样才能扩展一个javascript对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30912657/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com