On Wed, Mar 28, 2012 at 2:02 PM, Russell Leggett
<[email protected]>wrote:

> Ah, there you go. I figured I wasn't the first to think of it. I think it
> might be worth talking about this in relation to the shorthand function
> syntax, because it could pull the lexical this issue out of that debate.
> Then we could reduce the arrows to something simpler: -> for blocks, => for
> expressions with implicit return. It would also solve the related issue of
> this binding when passing methods as callbacks, which is also a major issue
> in a lot of apis, requiring extra parameters for the "this".
>

To me, the biggest problem when you need bound this is that you have to
keep around the bound version so you can remove it.  For example:

elem.addEventListener('click', onClick.bind(this));
...
elem.removeEventListener(type, ???);

I can't remove the bound listener unless I saved a reference to it, since
another bind with the same callback and object reference gets a new bound
function.  This adds a lot of extra work, and makes it easy to leak memory.

If the function already had a bound this by the way it was defined, I don't
need to keep an extra reference around (this assumes some class syntax that
allows the same function shorthands:

class Foo {
  register(elem) {
    elem.addEventListener('click', onClick);
  }

  unregister(elem) {
    elem.removeEventListener('click', onClick);
  }

  onClick(e) => do {
    ...
  }
}

-- 
John A. Tamplin
Software Engineer (GWT), Google
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to