Description
Over the past years, my personal preference on functions vs. arrow functions has shifted towards arrow functions. I used to be against arrow functions as a general replacement as they behave different than regular function
. Typical arguments against arrow functions in general were:
- They don't provide
arguments
this
is derived from the upper scope- They are not callable via
new
- They don't have a
prototype
- They are not hoisted, but are only available after evaluation
- They appeared as
anonymous function
in stack traces (.name
wasundefined
)
But now I've changed my mind:
-
With rest parameters, arrow functions can have
arguments
. Rest parameters are better because they are actual arrays. And in general, I think that all thesearguments
and rest parameters tricks can be replaced with regular array arguments which are more explicit and expressive anyway. -
I never use
this
in regular functions. Functions that usethis
should be part of aclass
where you can use method shorthands without thefunction
keyword. A function that usesthis
and that is not part of aclass
should rather accept an explicit argument than an implicitthis
(we already enforce this withno-invalid-this
). -
This is a good feature. If a
function
should be callable withnew
, it should be aclass
-
Same as above
-
This is never a real issue. If it is, it can be replaced with simpler and more explicit code.Not true. This may become an issue when evaluating modules that are part of a cyclic dependency graph. -
This is not true anymore. JavaScript engines derive the function name from the variable name:
Benefits of preferring arrow functions:
- Not accidentally callable with
new
(already mentioned) - They discourage strange usage of
this
(already mentioned) - Unified function style (= no special rules that arrow functions are only appropriate in certain scenarios)
- Shorter one line functions (no
return
) which are very common in functional programming patterns like Redux' action creators (see also Allow one line arrow functions #22)
The two last arguments are the most important ones for me that motivated me to change the rule. What do you think about it?