On Wed, Mar 28, 2012 at 2:38 PM, John Tamplin <[email protected]> wrote:

> 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 {
>     ...
>   }
> }
>

I think that's a very specific use case. Not that its uncommon, its just
that bound this is quite common in my experience without needing to hold
the reference. One possibility for your case, though, would be if the bind
operator always returned the same function. That would make it different
than the way the bind function works, but it has a certain amount of sense.
If this.onClick always refers to the same function, why not ::this.onClick?
Of course I know that gets a little tricky. What is it returning after all?
Do we create some concept of a method pointer? Something like C# delegates?
C# delegates can wrap a static function or instance method and can use ==
to compare equality.

(Regarding the strawman syntax, I imagine the :: is going to have conflict
with guards, no?)

- Russ





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

Reply via email to