Flex - 为对象属性名称使用变量 [英] Flex - Use Variables for Object Attribute Names

查看:17
本文介绍了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];

这篇关于Flex - 为对象属性名称使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆