Flex的 - 使用对象的属性名变量变量、属性、对象、Flex

2023-09-08 14:43:20 作者:[ 莫失初心 ]

如何使用变量访问对象的属性?

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;

在code以上对待字段名和fieldAge作为属性名称本身。 欲处理的相同的变量,并且映射与变量作为对象属性名称相关联的值。

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];