Alternative to Function.bind

Brendan Eich brendan at mozilla.com
Fri Mar 27 10:35:06 PDT 2009


On Mar 22, 2009, at 8:11 PM, Igor Bukanov wrote:

> Various current proposals for Function.bind have a drawback that in a
> typical situation one needs to use the reference to the object twice
> as in
>
>  obj.method.bind(obj)

This is common, but not majority-case from a quick survey:

$ find dojotoolkit.org MochiKit prototypejs.org -name '*.js' -print |  
xargs fgrep .bind\( | wc -l
       40
$ find dojotoolkit.org MochiKit prototypejs.org -name '*.js' -print |  
xargs fgrep .bind\( | /tmp/findbind.pl | wc -l
       18

where findbind.pl looks for /\b(\w+)\.\w+\.bind\(\1[,)]/  
(obj.method.bind(obj...).

Anyway, bind is being standardized based on the de-facto standard, the  
common intersection semantics among popular Ajax libraries' bind-like  
methods (jQuery has a different bind so I left it out of my find above).

>  obj.bind.method
>
> With such short form the need, for example, for Array methods taking
> an extra thisObj argument would be pretty much removed.

This is cool, although perhaps the name should be something else:

obj.bindSelfTo.method

or just

obj.bindTo.method

Don't want to get all long-winded, but do want to avoid overloading  
the ever-popular "bind".

/be


More information about the Es-discuss mailing list