我需要创建变量对象属性名称以用于数据网格组件。
这有效:
data = new Object();
data.some_name = "the data";
但这不是:
data = new Object();
colName = "some_name";
data[colName] = "the data";
任何人都可以帮助我吗?对象属性名称可以变量吗?
答案 0 :(得分:6)
var colName:String = "Column Title";
var colNameNoSpace:String = "ColumnTitle"
var dataObject:Object = new Object();
dataObject[colName] = "What's the problem?";
dataObject[colNameNoSpace] = "There's no problem!"
trace(dataObject["Column Title"]); //What's the problem?
trace(dataObject[colName]); //What's the problem?
trace(dataObject.ColumnTitle); //There's no problem!
trace(dataObject[colNameNoSpace]); //There's no problem!
答案 1 :(得分:1)
也许您忘了分配some_name属性!以下应该有效...
var data:Object = new Object();
data.some_name = "the data";
colName = "some_name";
data[colName] = "the data";
答案 2 :(得分:0)
它不起作用,因为:
colName是一个变量,表示指向放置字符串“some_name”的地址的指针