Actionscript 3:遍历/读取对象变量,可能吗?

发布于 2024-07-30 03:30:59 字数 727 浏览 3 评论 0原文

对于开源项目,我正在寻找一种将多个变量发送到方法的方法。 例如,这些变量是我想要直接传递到我正在创建的对象上的变量。

我认为对象是一个很好的方法,因为我可以发送任意数量的变量,而不必事先考虑每个可能的变量并创建一个接受 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戏蝶舞 2024-08-06 03:31:00

您可以使用 for(var prop:String in obj) 循环查看对象属性。 ...args 参数允许您将未定义数量的变量传递给方法:

public function test()
{
    var bar:Object = {x:1, y:2, z:3};
    var baz:Object = {a:"one", b:"two", c:"three"};
    foo(bar, baz);
}

public function foo(...args):void {
    for(var i:int = 0; i<args.length; i++) {
        for(var s:String in args[i]) {
        trace("prop :: "+s);
        trace("value :: "+args[i][s]);
        }
    }
}

prints

prop :: z
value :: 3
prop :: x
value :: 1
prop :: y
value :: 2
prop :: b
value :: two
prop :: c
value :: three
prop :: a
value :: one

为此您会受到一些性能影响,但有时这正是所需要的。

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:

public function test()
{
    var bar:Object = {x:1, y:2, z:3};
    var baz:Object = {a:"one", b:"two", c:"three"};
    foo(bar, baz);
}

public function foo(...args):void {
    for(var i:int = 0; i<args.length; i++) {
        for(var s:String in args[i]) {
        trace("prop :: "+s);
        trace("value :: "+args[i][s]);
        }
    }
}

prints

prop :: z
value :: 3
prop :: x
value :: 1
prop :: y
value :: 2
prop :: b
value :: two
prop :: c
value :: three
prop :: a
value :: one

You take a bit of a performance hit for this, but sometimes it's what's needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文