Flex - 为对象属性名称使用变量
问题描述:
如何使用变量访问对象属性?
How do you use variables to access Object attributes?
假设我有一个如下声明的对象,
Suppose I have an Object declared as follows,
var obj:Object = new Object;
obj.Name = "MyName";
obj.Age = "10";
我该怎么做这样的事情,
How would i do something like this,
var fieldName:String = "Name";
var fieldAge:String = "Age";
var Name_Age:String = obj.fieldName + " ," + obj.fieldAge;
上面的代码将fieldName"和fieldAge"视为属性名称本身.我想把它当作一个变量,并将与该变量关联的值映射为 Object 属性名称.
The code above treats 'fieldName' and 'fieldAge' as attribute name itself. I want to treat the same as a variable, and map the value associated with the variable as the Object attribute name.
答
只需像这样使用方括号:
Just use square brackets like this:
var age:String = obj[fieldAge];