开发者

Is there a way to pass the member function of another class as a parameter to a function in as3?

开发者 https://www.devze.com 2023-03-08 20:16 出处:网络
For example, I have a function in class A: private function functionA(f:Function):void { var objB:B; objB.f();

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消