Harold B. Harold B. -4 years ago 126
Javascript Question

Haxe Javascript: Prevent '$bind' generation?

Is there a simple way to prevent $bind generation when passing around class member functions?

I have an object that takes in a function which will be called in an arbitrary interval. The object always binds itself as the 'this' binding before calling the function (this is done in the native side), therefore the call to $bind itself is unnecessary. However, I can't seem to find a simple way to prevent $bind from being emitted any time I grab a member function by value.

The only way I've found is to use __js__ with a string literal of the member function name, which I would rather avoid... Is there a typed way to do so? Or something a bit nicer? A way to still use haxe syntax w/ identifiers instead of a string literal?


private function onSpawn():Void
this.setAct( act ); // Will generate JS: this.setAct($bind(this,this.act));
// Id like to simply have it generate: this.setAct( this.act );

// Mitigated like this:
this.setAct( untyped __js__("this.act") );

private function act( dt:Float ):Void

Thank you.

Answer Source

You use macro to mask out the untyped expression, but this is quite dangerous. Any reference to "this" will fail.


Btw, I think the compiler may be optimized to not generate $bind if the function code doesn't involve "this". You may want to raise an issue in the github repo about that.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download