开发者

Flex - Use Variables for Object Attribute Names

开发者 https://www.devze.com 2022-12-23 23:58 出处:网络
How do you use variables to access Object attributes? Suppose I have an Object declared as follows, var obj:Object = new Object;

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 lik开发者_开发问答e this,

var fieldName:String = "Name";
var fieldAge:String = "Age";
var Name_Age:String = obj.fieldName + " ," + obj.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];
0

精彩评论

暂无评论...
验证码 换一张
取 消