我有一个参数为字符串的函数.我想将此字符串用作对象中的属性.这是一个示例:
var x = "somestring"
function foo(attribute) {
someobj.attribute = "something";
}
foo(x);
解决方法:
对动态键使用下标[]表示法:
someobj[attribute] = "something";
码
var x = "somestring";
var someobj = {};
function foo(attribute) {
someobj[attribute] = "something";
}
foo(x);