0

I want to see if my understanding is correct: the functionality of the function keyword was divided up between classes and Arrow functions in es6.

First, in the case of anonymous functions, I see no advantage in using function() {...} over Arrow functions, especially given the this binding in Arrow functions.

For non-anonymous functions that are not constructors, they can be declared as Arrow functions bound to a name. Given the differences in hoisting and perhaps readability, I suppose it is somewhat a matter of opinion whether using Arrow functions is strictly superior.

For functions that are constructors, classes may be used instead.

So is it correct by modern practices to say that the function keyword need never be used, or even more strongly, should never be used?

EDIT: To be clear, I am aware of the differences between function declarations, function expressions, and Arrow functions. I am not meaning to suggest that we can go through non es6 JS and blindly replace functions decs/exprs with Arrow functions. My question is whether when writing new applications in JavaScript, if there is ever a reason to use the function keyword, and if it my understanding is correct that the utility of the function keyword can be thought of as split between classes and Arrow functions.

Samasambo
  • 113
  • 7
  • 1
    Nope. Sometimes you want to capture `this`, sometimes you don't. – CertainPerformance Jul 31 '18 at 00:46
  • Like I said, sometimes you want to capture a function's calling context, and sometimes you want to inherit the calling context of the current block. Restricting yourself to only one method or the other will reduce a programmer's abilities to write short, clean code. – CertainPerformance Jul 31 '18 at 00:53
  • 1
    @CertainPerformance I was looking at the answer to the question you linked, and it says this `But: If the code which calls the callback explicitly sets this to a specific value, as is often the case with event handlers, especially with jQuery, and the callback uses this (or arguments), you cannot use an arrow function!` Is this a scenario you were talking about? I don't quite understand the scenario they're describing. – Samasambo Jul 31 '18 at 00:55
  • 1
    @Samasambo Main point is that you as the programmer may want `this` to point to the level above the current block and not the current one. The Arrow functions `fix` the usual `var self = this, .bind(this)` and other `tricks` to make the executing context `this` and not the one above it. They are not by any means fix for everything and should only be used instead of `function`. Here is more: https://medium.com/tfogo/advantages-and-pitfalls-of-arrow-functions-a16f0835799e – Akrion Jul 31 '18 at 01:19
  • @Akrion Ah yes I see what you're saying. At least for object methods (the example given in that medium post), you could use the es6 shorthand to not type `function` (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions); while functionally the same, the function keyword is omitted. – Samasambo Jul 31 '18 at 17:46

0 Answers0