0

In ES6, we were given the ability to do arrow function expressions:

let apples = () => { ...

But nothing was done with function declarations:

function apples() { ...

Given the hoisting differences between the two types of functions, is it still proper to use 'function apples() { ...' for functions in ES6 code?

J Seabolt
  • 1,790
  • 3
  • 16
  • 41
  • 1
    In my opinion you should only use your first style `let fnName = () => {}` when you need the `this` pointer guaranteed. A general purpose `function` is just a function and unless it needs the `this`pointer guaranteed stick with #2 `function fnName() {}` The Arrow function is great for anonymous functions but remember that whatever the `this` pointer is when the function is declared is what the this pointer will be within the arrow function. – Intervalia Dec 02 '17 at 20:54
  • 1
    Here is a good article that explains some of the hidden gotchas with arrow functions and why I don't use them as often as others: http://wesbos.com/arrow-function-no-no/ (No, I didn't write it.) – Intervalia Dec 02 '17 at 21:00

1 Answers1

1

Simple answer: Yes.

Just like the various tools in a toolbox you use the right tool for the right job. Standard functions are still valid and probably will be as long as JavaScript exists.

Arrow functions are awesome when used correctly. But I often see people using them just to stop typing the word function. I use arrow functions when it make sense, but I also use function.

To me this:

let fnName = () => {}

seems silly and this:

let fnName = function fnName() {}

is even sillier, yet I see it all the time.

You really have to be aware of the this pointer on arrow functions since they are the same as the code that defined them at the time they were defined.

As I mentioned in my response above here is a good article on some reasons to not use arrow functions:

And here are two other articles on the use of arrow functions:

Intervalia
  • 8,536
  • 1
  • 21
  • 47