从另一个类传递的回调

时间:2015-09-15 22:50:03

标签: haxe

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

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

public static var onFocusCallback:Dynamic;

public static function triggerFocus():Void
{
    onFocusCallback.bind();
}

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

2 个答案:

答案 0 :(得分: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();
    }
}

答案 1 :(得分:-1)

在发布问题的几分钟内,我开始工作的另一个解决方案就是使用Reflect

Reflect.callMethod(null, onFocusCallback, []);