Actionscript 3:遍历/读取对象变量,可能吗?
对于开源项目,我正在寻找一种将多个变量发送到方法的方法。 例如,这些变量是我想要直接传递到我正在创建的对象上的变量。
我认为对象是一个很好的方法,因为我可以发送任意数量的变量,而不必事先考虑每个可能的变量并创建一个接受 40 多个变量的方法。 那有点糟糕。
但问题是,我不知道如何“遍历”一个对象并找到它的所有变量。 使用数组,这很容易做到,但我无法轻松地用它发送变量的名称。
为了澄清起见,举个例子:
public function create(settings:Object=undefined):void{
var item:Panel = new Panel();
/*
the idea is that 'settings' should contain something like:
settings.width=300;
settings.height=500;
settings.visible=false;
and I want to be able to walk through the provided variables,
and in this case inject them into 'item'.
*/
}
有办法做到这一点吗? 或者我使用 Object 的想法是错误的,我应该选择使用其他解决方案吗? 请指教。
提前谢谢了!
-DJ
For an open source project I am looking for a way to send multiple variables to methods. These variables are, for example, variables I want to directly pass onto an object I'm creating.
I figured an object would be a good way to go, as I can send as many variables as I like without having to think about every possible variable beforehand and creating a method that accepts 40+ variables. That would kinda suck.
The problem is, though, I have no idea how I can 'walk' through an object and find all it's variables. With an Array this is simple to do, but I can't easily sent the variable's name with it.
For clarification, an example:
public function create(settings:Object=undefined):void{
var item:Panel = new Panel();
/*
the idea is that 'settings' should contain something like:
settings.width=300;
settings.height=500;
settings.visible=false;
and I want to be able to walk through the provided variables,
and in this case inject them into 'item'.
*/
}
Is there anyway to do this? Or is my idea of using Object wrong, and should I opt to use another solution? Please advice.
Many thanks in advance!
-DJ
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 for(var prop:String in obj) 循环查看对象属性。 ...args 参数允许您将未定义数量的变量传递给方法:
prints
为此您会受到一些性能影响,但有时这正是所需要的。
You can look through an objects properties using a for(var prop:String in obj) loop. The ...args parameter allows you to pass undefined numbers of variables to a method:
prints
You take a bit of a performance hit for this, but sometimes it's what's needed.