当属性名称来自var?
时,如何向对象添加属性var obj={};
var propName="realName";
obj.propName; //wrong
obj."realName"; //wrong
答案 0 :(得分:5)
您可以使用square bracket notation:
var obj = {};
var propName = "realName";
obj[propName] = "foo"; // Same as `obj.realName = "foo";`