3

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 such syntax exists in c++, but not sure if you can do this in flex/as3

Bubbles
  • 466
  • 4
  • 17

1 Answers1

8

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.

Richard Szalay
  • 83,269
  • 19
  • 178
  • 237