'Does Haxe support function.apply()?
I have a question about Haxe, does Haxe support method.apply(this, paramArray) that is similar to Javascript? Thanks.
Best, Peter Zhou
Solution 1:[1]
Is the method Reflect.callMethod or you need something else?
Solution 2:[2]
You can use following code to ease conversion. However, you can't use thisArg like in AS3 because callMethod ignores first argument (o:Dynamic): pay attention to second trace call.
using Main.StaticExtender;
class Main {
public var value:Int;
function new() {}
function foo(x:Int): Int {
return value = x;
}
static public function main() {
var m = new Main();
var m2 = new Main();
trace(m.foo.apply(m,[273998236]));
trace(m2.foo.apply(m2,[273998237]));
trace(m.value);
trace(m2.value);
}
}
class StaticExtender {
static public function apply(f:Dynamic,o:Dynamic,a:Array<Dynamic>):Dynamic {
return Reflect.callMethod(o,f,a);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andrew |
| Solution 2 | Ilir Liburn |
