我有以下 JavaScript 代码/对象:
config = function(key) {
props = {
svgWidth: 500,
svgHeight: 500,
userUploadImgWidth: 1115,
userUploadImgHeight: 528,
};
return props[key];
}
我想向 probs
对象添加额外的键值对,创建如下内容:
config = function(key) {
props = {
svgWidth: 500,
svgHeight: 500,
userUploadImgWidth: 1115,
userUploadImgHeight: 528,
scaledDimensions: {
scaleWidth: 500,
scaleWidth: 232
}
};
return props[key];
}
我似乎可以访问函数内的 props 对象,它将使用 config('userUploadImgWidth')
返回值,但我想向该对象添加新项目,但无法弄清楚语法。
最佳答案
完全没有必要用它来创建一个函数。您确定您没有过度设计一些非常微不足道的东西吗?
出了什么问题
config = {
svgWidth: 500,
svgHeight: 500,
userUploadImgWidth: 1115,
userUploadImgHeight: 528,
scaledDimensions: {
scaleWidth: 500,
scaleWidth: 232
}
}
及以后。
something = config.userUploadImgWidth;
关于javascript - 将项目添加到函数内的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44221213/