r/javascript • • Mar 31 '17

ES2018 First Look - it changes everything

https://www.youtube.com/watch?v=s-G_RZ4RJLU
464 Upvotes

120 comments sorted by

View all comments

Show parent comments

20

u/ajacksified Apr 01 '17

() => 'welcome to the future'

7

u/azangru Apr 01 '17

Some people dislike anonymous functions (cough — Kyle Simpson — cough)

Some people like function hoisting.

¯_ (ツ) _/¯

6

u/[deleted] Apr 01 '17

If we're just writing a normal, ordinary function, what benefit does

const myFunction = () => { doStuff; }

have over

function myFunction() { doStuff; }

assuming the function doesn't invoke this?

To put it another way - other than using shiny new features for the sake of using shiny new features (which is, honestly, the sort of thing I would do), why change how you write ordinary functions?

2

u/[deleted] Apr 01 '17 edited Apr 01 '17

assuming the function doesn't invoke this?

That's the crucial detail. I prefer arrow functions because they leave no ambiguity regards this; the context of this only changes when you explicitly want it to and use the function keyword.

Also, I like how terse they are and how they allow shorthand returns. Compare these:

function multiply (a, b) { return a * b }
// or (see below)
const multiply = function multiply (a, b) { return a * b }

Or:

const multiply = (a, b) => a * b

Also, consider hoisting. If you don't want hoisting (I don't generally) then you'll have to define the function name twice if using the function keyword, but only once with arrow functions.

0

u/[deleted] Apr 01 '17

[deleted]

2

u/[deleted] Apr 01 '17

Terminology may not be correct, but in practice the first one defines the function (non-hoisted) and the second ensures it isn't anonymous with regards console logs/errors.

See: https://github.com/airbnb/javascript#functions