I actually listened to a fresh interview with Brendan Eich yesterday, where he said that the keyword function he chose when he was prototyping JavaScript turned out to be a bit on the longish side for the JavaScript parser, which negatively affected its speed; so when the video mentioned the shortcut for the function keyword, it sounded a little too real.
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?
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.
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.
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.
25
u/azangru Apr 01 '17
I actually listened to a fresh interview with Brendan Eich yesterday, where he said that the keyword
functionhe chose when he was prototyping JavaScript turned out to be a bit on the longish side for the JavaScript parser, which negatively affected its speed; so when the video mentioned the shortcut for thefunctionkeyword, it sounded a little too real.