将函数传递给Actionscript 3中的另一个函数

时间:2022-06-05 16:03:05

I have a function that passes an array to another function as an argument, there will be multiple data types in this array but I want to know how to pass a function or a reference to a function so the other function can call it at any time.

我有一个函数将数组作为参数传递给另一个函数,这个数组中会有多个数据类型,但我想知道如何传递函数或函数的引用,以便其他函数可以随时调用它。

ex.

function A:

add(new Array("hello", some function));

function B:

public function b(args:Array) {
    var myString = args[0];
    var myFunc = args[1];
}

3 个解决方案

#1


28  

Simply pass the function name as an argument, no, just like in AS2 or JavaScript?

只需将函数名称作为参数传递,不,就像在AS2或JavaScript中一样?

function functionToPass()
{
}

function otherFunction( f:Function )
{
    // passed-in function available here
    f();
}

otherFunction( functionToPass );

#2


6  

This is very easy in ActionScript:

这在ActionScript中非常简单:

function someFunction(foo, bar) {
   ...
}

function a() {
    b(["hello", someFunction]);
}

function b(args:Array) {
    var myFunc:Function = args[1];
    myFunc(123, "helloworld");
}

#3


2  

You can do the following:

您可以执行以下操作:

add(["string", function():void
{
trace('Code...');
}]);

...or...

...
add(["string", someFunction]);
...

private function someFunction():void
{
trace('Code...');
}

#1


28  

Simply pass the function name as an argument, no, just like in AS2 or JavaScript?

只需将函数名称作为参数传递,不,就像在AS2或JavaScript中一样?

function functionToPass()
{
}

function otherFunction( f:Function )
{
    // passed-in function available here
    f();
}

otherFunction( functionToPass );

#2


6  

This is very easy in ActionScript:

这在ActionScript中非常简单:

function someFunction(foo, bar) {
   ...
}

function a() {
    b(["hello", someFunction]);
}

function b(args:Array) {
    var myFunc:Function = args[1];
    myFunc(123, "helloworld");
}

#3


2  

You can do the following:

您可以执行以下操作:

add(["string", function():void
{
trace('Code...');
}]);

...or...

...
add(["string", someFunction]);
...

private function someFunction():void
{
trace('Code...');
}