arrow function syntax simplified

Claus Reinke claus.reinke at
Wed Apr 4 06:55:26 PDT 2012

>>> Other than the JQuery style misuse of this, what are the 
>>> use cases? If you want to bind this, why wouldn't a method 
>>> invocation on an objet be involved?
>> The ES array loops accept an optional this parameter to
>> be used for the loop callback.
> It is up to the caller of the forEach to provide both the callback 
> function and the optional this arg.  So the caller has the option 
> on both the kind of function passed and whether a this value is
> needed.  
> ..
> in other-words, it is available as an alternative to saying:
>   logAll: function() {
>      var self = this;
>      this.state.forEach(function(v) {self.log(v)});
>   }
> ..
> Arrow functions need to eliminate the need for such idioms 
> that get around unwanted dynamic this bindings.  

Another way to look at these higher-order methods is that they
split plain methods into framework (eg loop) and callbacks (eg
loop body). Only the framework is directly attached to the object
(eg Array), while the callbacks are passed in from the outside.

Passing the framework this to the callback brings framework 
and callback together again. One can write the callback as if it 
was part of the object method.

> No, there's nothing wrong with your wrappering, but I'm not 
> sure why it would be needed.  You code code the above as:
>   logAll: function() {this.state.forEach(fn((it,v)-> it.log(v)),this)};
> but why would you? Particularly since you have to name the 
> explicit initial this-like argument in the arrow something other 
> than "this".

I'm not saying that this is needed, only that we can account for
this use case without changing arrow functions. And I was 
surprised that both pro and cons camps continued the discussion 
of recursive self and dynamic this naming as if no workaround 
was available.

To me, the ability to emulate the feature additions so closely in
library functions suggests that some variation of fn and rec 
should go into the standard library instead of adding such 
features to arrow functions. But perhaps there are reasons for
preferring the language features over the library functions?



More information about the es-discuss mailing list