您如何获得在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.
最佳答案
尽可能不要使用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();
}
}