I need to create variable object property names for use with the data grid component.
This works:
data = new Object();
data.some_name = "the data";
But this does not:
data = new Object();
colName = "some_name";
data[colName] = "the data";
Can anyone help me? Can object property names be variable?开发者_开发知识库
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!
Maybe you forgot to assign the some_name property! the following should work...
var data:Object = new Object();
data.some_name = "the data";
colName = "some_name";
data[colName] = "the data";
It does not work because:
colName is a variable, which means is a pointer to the address where the string "some_name" is placed
精彩评论