Improving Function.prototype.bind

Andrea Giammarchi andrea.giammarchi at
Fri Jan 6 03:23:57 PST 2012

it doesn't ... as soon as you release the reference to o no leaks persists
but of course until you keep o on hold those unique callbacks cannot be
released ... but this would be true with WeakMap too, isn't it?

In any case, boundTo is suitable for listeners and the whole point is to do
not hold manually those function

once again

window.addEventListener("whatever", o.boundTo(o.method), false);
// later on
window.removeEventListener("whatever", o.boundTo(o.method), false);
// that's it

We can reuse/add the listener later on without problems but as soon as
object "o" will be unreachable (no reference count === 0) everything will
be fine

This, versus this anti pattern

o._leMethodBound = o.method.bind(o);
window.addEventListener("whatever", o._leMethodBound, false);
// later on
window.removeEventListener("whatever", o._leMethodBound, false);

o._leMethodBound is exposed and used only to hold a bound method ... we
have all done this until now, and to me it's kinda illogical, boring, error


On Fri, Jan 6, 2012 at 11:41 AM, David Bruant <bruant.d at> wrote:
> In your implementation, you store in an array references to functions, not
> to objects.
> I would guess that
> -----
> var o = {};
> for (var i = 0, a = []; i < 0xFFFF; i++) a.push(o.boundTo(function(){}));
> a = null;
> -----
> leaks. Does it?
> I certainly leaks, because references to the functions accumulate in
> 'cbStack' and are never free'd.
> David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list