I hate javascript's "$bind"

80 views
Skip to first unread message

我每天睡不着

unread,
May 2, 2016, 12:52:38 AM5/2/16
to Haxe
js.Browser.document.onclick = function(e:js.html.KeyboardEvent){
    if (e.stopPropagation != null) e.stopPropagation();   // for ie
}

output:

window.document.onclick = function(e) {
    if($bind(e,e.stopPropagation) != null) {
        e.stopPropagation();
    }
};


for extern class, why not remove the "$bind" to get the cleaner output???

and for js-es5, why not just using `Function.prototype.bind` instead "$bind" ???

Juraj Kirchheim

unread,
May 2, 2016, 4:05:37 AM5/2/16
to haxe...@googlegroups.com
On Mon, May 2, 2016 at 6:52 AM, 我每天睡不着 <sen...@gmail.com> wrote:

for extern class, why not remove the "$bind" to get the cleaner output???

1. An extern class needn't be native. It could also be a haxe generated class from a different haxe/js file.
2. Simply because `this` gets lost and generally that's undesired.

and for js-es5, why not just using `Function.prototype.bind` instead "$bind" ???

Because if `e.stopPropagation` is `null` then `e.stopPropagation.bind(e)` will give you a null pointer exception.

It can be argued that when checking for the existence of a function, the `$bind` is superfluous and should not be generated at all. You can try raising an issue on GitHub. Personally, I think you'll be better off just ignoring this detail ;)

Best,
Juraj

Philippe Elsass

unread,
May 2, 2016, 4:58:09 AM5/2/16
to Haxe

There's also an important feature of $bind: it always return the same reference (for the same function and scope), which allows you to addListener/removeListener without saving the result of the binding, which would be very confusing as you never write the binding code yourself.

However you're right that in your example the $bind is superfluous.

Philippe

--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages