使用 ...(rest) 参数将参数从数组传递到 Actionscript 方法 [英] Pass arguments from an array to an Actionscript method with ...(rest) argument

查看:22
本文介绍了使用 ...(rest) 参数将参数从数组传递到 Actionscript 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

my question is the Flex transposition of this question :

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

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

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).

一些代码来说明:

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?

推荐答案

可以通过检查 Function 对象本身来实现.调用 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
}

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

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

这篇关于使用 ...(rest) 参数将参数从数组传递到 Actionscript 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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