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

David Bruant bruant.d at gmail.com
Wed Oct 2 00:34:44 PDT 2013


Le 02/10/2013 04:35, Andrea Giammarchi a écrit :
> setTimeout accept extra arguments ... I write JavaScript that uses 
> this feature.
>
> `setTimeout(callback, delay, arg1, arg2, argN, evenAnObject);`
What is "evenAnObject"? It doesn't look like a standard thing: 
http://www.whatwg.org/specs/web-apps/current-work/multipage/timers.html#windowtimers

>
> so fat arrow does not solve much here, I can use self as first 
> argument and I am good.
>
> `forEach` and all other arrays accept a second argument
>
> `array.forEach(doStuff, boundContextObject);`
>
> so fat arrow does not solve a thing in mostly all Array extras.
Both examples are most certainly design mistakes. It feels absurd that 
every function accepting a function as argument must also have a second 
argument for the |this| value (or even variable number of arguments to 
be passed around). Especially in the ES5 era with Function.prototype.bind.
The design mistake is emphasized by the inconsistency in the order of 
arguments in the 2 examples you provide.
I'm glad Node.js didn't make all async functions accept another argument 
for |this|.

> for **DOM** I use handlers as specified by **W3C**
Since when the DOM is specified by W3C? Must be about 10 years it isn't 
the case anymore</troll> ;-)
Passing a function directly is standard too.

> so that `{handleEvent: function () {this}}` works better than any mess 
> I could create with callbacks that I won't be unable to remove later 
> on (as I've said) ... so I can use `removeEventListener(this)` in 
> every method handled by that object.
Interesting trick.
But "won't be able to remove later on" is false:

     document.addEventListener('DOMContentLoaded', function dcl(){
         // whatev's
         document.removeEventListener('DOMContentLoaded', dcl);
     });

>
> So I actually wonder what kind of JavaScript **you** write because 
> this was a honest question but probably ... people not familiar with 
> JS are the answer: since developers ignore part of JS specs available 
> since every then we need a fat arrow to break old syntax to make the 
> creation of self bound function easier.
One of the reason of arrow functions to exist is to get rid of the need 
for "var self = this" or equivalent ('that', '_this', etc.). Example on MDN:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/arrow_functions#Lexical_this

I understand that in some cases, specs allow to pass |this| or 
additional arguments, but it doesn't make it a good idea. I see the fact 
that Node.js didn't take that road as a very strong signal.

David


More information about the es-discuss mailing list