what kind of problem is this fat arrow feature trying to solve ?

Andrea Giammarchi andrea.giammarchi at gmail.com
Wed Oct 2 11:11:02 PDT 2013


You need to be sure about the `this` because today you can `bind(anyObject
|| this)` while with fat arrow you can only bind `this` so you need to be
sure that the function context, the one everybody has always borrowed
through call and apply, is the one you expect to be.

In a new direction where instanceof is being discouraged, the usage of this
become critical.

As shortcut, fat arrow fails. `var o = {method: () => whatver;};`

As DOM listener it brings what `handleEvent` does since ever.
```javascript
var o = {
  init: function () {
    document.addEventListener('click', this);
    // virtually the same of
    document.addEventListener('click', (e) => this.handleEvent(e));
  },
  handleEvent: function () {
    this === o;
  }
};
```

So I don't see many advantages on DOM world.

Once again, I am trying to understand where is the glory of this new
feature.

I am not saying it should not be there, ES6 classes and generators already
breaks the old syntax so it won't hurt a new utility as fat arrow is but
where this will be exactly a better approach to the current situation ?

The only one that comes into my mind is the `once` event listener in node
or eddy.js ... but `once` a part, and I admit in there it's handy, I cannot
see many other real/concrete usages.

In CoffeeScript the fat arrow behaves differently having a dynamic this, am
I correct?
So whoever is comparing fat arrow with CoffeeScript should be aware these
are completely different beasts and there was my question about what is
solving for the real world.

No need to talk about use strict.

Thanks





On Wed, Oct 2, 2013 at 11:01 AM, Brendan Eich <brendan at mozilla.com> wrote:

> Andrea Giammarchi wrote:
>
>> fat arrow does not add any capabilities but create the need to be sure
>> about the context.
>>
>
> This does not make sense. The problem of being unsure about "which this"
> already exists. Adding arrows does not make it worse. The new syntax makes
> it better, compared to 'var self = this;' or .bind(this) hacks that are
> more verbose and easy to forget -- that is, whose absence is easy to
> overlook.
>
>
>  In use strict fat arrow will bring a lovely `undefined` wich is undesired
>> so nothing is solved in there.
>>
>
> Stop comparing apples to oranges. If we have today
>
> "use strict";
> function foo() {
>   ...
>   setCallback(function () { ... this ...}.bind(this));
> }
>
>
> Then you have no greater or lesser incidence of undefined-this bugs due to
> someone calling foo() instead of foo.call, foo.apply, or obj =
> {method:foo}, obj.method() by changing this code to use an arrow:
>
> "use strict";
> function foo() {
>   ...
>   setCallback(() => {... this ...});
> }
>
>
> If you don't like strict mode, keep that out of this thread. Incidence of
> undefined-this bugs due to it do not change with arrows.
>
> /be
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20131002/c2e35ea4/attachment.html>


More information about the es-discuss mailing list