For example, I have a function in class A:
private function functionA(f:Function):void
{
var objB:B;
objB.f();
}
Is there a way to pass a non-static public member function of class B as a parameter to functionA? (from inside class A, of course) I know su开发者_StackOverflowch syntax exists in c++, but not sure if you can do this in flex/as3
Sure:
var a : A = new A();
var b : B = new B();
a.functionA(b.functionB);
...
private function functionA(f:Function):void
{
f();
// or
f(1, "hi");
}
The instance associated with the function is carried with it. If you need to call the function on a different instance call f.apply(instance, [1, "hi"])
AS3 has no concept of delegates or function-signatures-as-a-type so you'll need to know the arguments to pass in.
精彩评论