gpt4 book ai didi

javascript - javascript中的封装

转载 作者:行者123 更新时间:2023-11-28 11:59:51 26 4
gpt4 key购买 nike

如何从下面获取types.type1["color"]?我的意思是如何编写公共(public)方法来获取它。

Config = (function(){

var ids = {
id1: "id1",
id2: "id2",
....
}
var types = {
type1: {
"color": "red",
"size": "10"
...
},
type2: {
"color": "red",
"size": "40"
}
}
return {
getIds: function(id){
return ids[id];
}
getTypes: function(type){
return types[type];
}

}
}();

最佳答案

Config.getTypes("type1").color

使用:

Config = (function(){
var ids = {
id1: "id1",
id2: "id2"
};

var types = {
type1: {
"color": "red",
"size": "10"
},
type2: {
"color": "red",
"size": "40"
}
};

return {
getIds: function(id){
return ids[id];
},
getTypes: function(type){
return types[type];
}
};
})();

关于javascript - javascript中的封装,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17357154/

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