Improving Function.prototype.bind

Andrea Giammarchi andrea.giammarchi at gmail.com
Thu Jan 5 23:47:23 PST 2012


most JS developers use what their library/framework provide and the bind is
most of the time transparent.

Libraries developers simply use bind as a Santa Claus ES5 gift so they
don't bother thinking about the pattern they are forced to use, they just
use bind the way they can but again, not relying in fresh new objects per
context.

My code, in my blog, is using same WeakMap concept, except is not using
WeakMap ... I can hardly think of a browser that implements Harmony WeakMap
but not Function.prototype.bind yet

Finally, accordingly with @Mark reply, I think it's still not clear what I
am talking about ... so here the signature/spec:


Object.prototype.boundTo(callback:Function):BoundObject

The method accepts one argument only and this must be one with [[Call]]
capabilities (a Function/callable, nothing else)

The BoundObject is a singleton generated with the unique combination of the
current generic object that is calling the boundTo method, and the given
Function argument.

The behavior of this BoundObject is exactly the same of

callback.bind(genericObject)

except it's a singleton.

A smart implementation should be able to release this singleton once the
genericObject that invoked boundTo has reference counter equals to zero and
of course, if nothing else is holding the BoundObject explicitly ( but in
that case the reference counter of genericObject cannot be zero so ... )

If latest point is "too smart/complicated" then the signature would be

Object.prototype.boundTo(callback:Function[,
erase:Boolean=false]):BoundObject

where a call to

genericObject.boundTo(callback, false)

should be able to return the BoundObject in any case but destroy it
internally.
Latter point is mainly for shims and I think not needed.

Accordingly with this, everything that Mark wrote in his last reply could
not happen ... everything will work as expected.

Any better ?

I can try to explain even more, if necessary, or provide better
specifications aligned with current ES5 terminology

Best Regards,
    Andrea Giammarchi

P.S. is it really just me that see the problem with current bind ?

On Fri, Jan 6, 2012 at 2:22 AM, Brendan Eich <brendan at mozilla.com> wrote:

> On Jan 5, 2012, at 4:47 PM, Andrea Giammarchi wrote:
>
> Right now the lack of memoization is not a burning issue, from what I
> hear. You've raised it, I think for the first time on es-discuss.
>
> /be
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120106/815ec58f/attachment.html>


More information about the es-discuss mailing list