从另一个类传递的回调

Dan*_*iel 2 haxe

你如何在haxe 3中运行由另一个类传递的回调?

我正在尝试将回调函数传递给类,但是我收到了错误

public static var onFocusCallback:Dynamic;

public static function triggerFocus():Void
{
    onFocusCallback.bind();
}
Run Code Online (Sandbox Code Playgroud)

我得到的错误是 [Fault] exception, information=ReferenceError: Error #1069: Property bind not found on builtin.as$0.MethodClosure and there is no default value.

Jus*_*ado 6

尽量不要使用Dynamic.它可以触发像这样的奇怪错误.

使用回调的方法就像这样http://try.haxe.org/#60f45

class Test {
    static function main() {
        onFocusCallback = function() {
            trace("focus");
        }   
        triggerFocus();
    }

    // Try not to use Dynamic
    //public static var onFocusCallback:Dynamic;
    // If you don't know the type of the function, you can use this:
    //public static var onFocusCallback:haxe.Constraints.Function;
    // But it's always better to give a concrete type like:
    public static var onFocusCallback:Void->Void;


    public static function triggerFocus():Void
    {
        if(onFocusCallback != null) onFocusCallback();
    }
}
Run Code Online (Sandbox Code Playgroud)