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?

3

u/[deleted] Apr 01 '17

Yeah i'm baffled by that too. It doesn't make it more readable because you have to interpret the () => as being a function. And when you have some weird nesting going on because your application isn't only going to display "Hello world", it can become quite a mess and hard to read.

I can understand why a uglifier could translate it, because it simply has less characters, but i'm already using a tool that does that for me for release. But i don't know why one would want to use that for a developing state. Btw, you can also have:

const myFunction = function() { doStuff; }

Since you set one to a variable and the other is not.

Its the same argument i had about the whole shortening and dot syntax while watching. It only makes things shorter, not readable. Its also the reason why i never shorten a if else with the ? : syntax. It doesn't matter as i can let my compiler shorten it for me and i already have a ifelse + tab shortcut on my editor. Shortening for the sake of shortening is never going to make your code more readable. And if you do it to write less code yourself, you are doing it wrong. I get why a compiler would benefit from it, but a human should not.

1

u/fire_code !expert Apr 02 '17

I disagree on the ternary comment (? :).

It's very useful when assigning a value to a variable depending on a contextual variable/value. If you're using block-scoped variable declarations like let and const, that means that if you wanted the same functionality in an if/else statement, you'd need an undefined let variable declaration above the if/else block, which then allows that set variable to be susceptible to (direct) mutation, since it is not a const.

-1

u/Zero-Tau Apr 01 '17

To me it's more readable to use the arrow syntax simply because it's more standard: that's how most languages write it, so it's simpler and quicker to parse for me.

It's like if instead of [1,2,3] we wrote

Array 1 and 2 and 3 ArrayEnd

, then ES2016 added the square bracket syntax. People primarily used to JS might see that as shortening for the sake of shortening, and say you still have to parse it as array. But anyone who knew or used multiple languages would see that as more natural and easier to parse.

It's also simpler because the function keyword can be used in multiple configurations with different semantics but the arrow function feature always means the same thing.