使用 ...(rest) 参数将参数从数组传递到 Actionscript 方法
我的问题是这个问题的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通过遍历 Function 对象本身就可以实现这一点。对其调用 apply() 即可:
有关详细信息,请查看 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:
For more info, check out http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Function.html#apply()
编译器很难猜测你想要什么,你想要传递一个 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.
...args 是该方法等待的一个对象。您可以传递多个元素或(在本例中)带有参数的一个数组。
示例:
希望有帮助,
抢
The ...args is one Object the method awaits for. You can pass multiple elements or (in this case) one array with the parameters.
Example:
Hope it helps,
Rob