As3 flex中的变量对象属性名称?

时间:2010-12-03 06:50:28

标签: flex flash actionscript-3

我需要创建变量对象属性名称以用于数据网格组件。

这有效:

 data = new Object();
 data.some_name = "the data";

但这不是:

 data = new Object();
 colName = "some_name";
 data[colName] = "the data";

任何人都可以帮助我吗?对象属性名称可以变量吗?

3 个答案:

答案 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”的地址的指针