使用 ...(rest) 参数将参数从数组传递到 Actionscript 方法

发布于 2024-10-21 09:04:30 字数 652 浏览 3 评论 0原文

我的问题是这个问题的 Flex 换位:

我可以将数组作为参数传递给 Java 中具有可变参数的方法吗?

也就是说,我在某些 Actionscript 代码中有一个数组,并且需要传递数组中索引的每个对象进入方法method(...arguments)

一些代码可以清楚地表明:

private function mainMethod():void{
    var myArray:Array = new Array("1", "2", "3");
    // Call calledMethod and give it "1", "2" and "3" as arguments
}

private function calledMethod(...arguments):void{
    for each (argument:Object in arguments)
        trace(argument);
}

是否有某种方法可以按照评论的建议进行操作?

my question is the Flex transposition of this question :

Can I pass an array as arguments to a method with variable arguments in Java?

That is, I have an Array in some Actionscript code and i need to pass every object indexed in the array into a method method(...arguments).

Some code to make it clear:

private function mainMethod():void{
    var myArray:Array = new Array("1", "2", "3");
    // Call calledMethod and give it "1", "2" and "3" as arguments
}

private function calledMethod(...arguments):void{
    for each (argument:Object in arguments)
        trace(argument);
}

Is there some way to do what the comment suggests?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

半透明的墙 2024-10-28 09:04:30

通过遍历 Function 对象本身就可以实现这一点。对其调用 apply() 即可:

private function mainMethod():void
{
    var myArray:Array = new Array("1", "2", "3");

    // call calledMethod() and pass each object in myArray individually
    // and not as an array
    calledMethod.apply( this, myArray );
}

private function calledMethod( ... args ):void
{
    trace( args.length ); // traces 3
}

有关详细信息,请查看 http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Function.html#apply()

It's possible by going through the Function object itself. Calling apply() on it will work:

private function mainMethod():void
{
    var myArray:Array = new Array("1", "2", "3");

    // call calledMethod() and pass each object in myArray individually
    // and not as an array
    calledMethod.apply( this, myArray );
}

private function calledMethod( ... args ):void
{
    trace( args.length ); // traces 3
}

For more info, check out http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Function.html#apply()

呆° 2024-10-28 09:04:30

编译器很难猜测你想要什么,你想要传递一个 Array 类型的参数还是想要传递该数组的元素。编译器采用假设一。

It is kind of hard for the compiler to guess what you want, do you want to pass one argument of type Array or do you want to pass the elements of that array. The compiler goes for assumption one.

断肠人 2024-10-28 09:04:30

...args 是该方法等待的一个对象。您可以传递多个元素或(在本例中)带有参数的一个数组。

示例:

function mainMethod():void
{
    //Passing parameters as one object
    calledMethod([1, 2, 3]);

    //Passing parameters separately
    calledMethod(1, 2, 3);
}

function calledMethod(...args):void
{
    for each (var argument in args)
    {
        trace(argument);
    }
}

mainMethod();

希望有帮助,

The ...args is one Object the method awaits for. You can pass multiple elements or (in this case) one array with the parameters.

Example:

function mainMethod():void
{
    //Passing parameters as one object
    calledMethod([1, 2, 3]);

    //Passing parameters separately
    calledMethod(1, 2, 3);
}

function calledMethod(...args):void
{
    for each (var argument in args)
    {
        trace(argument);
    }
}

mainMethod();

Hope it helps,
Rob

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