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.
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.
Well you can't really reuse arrow functions unless you put it into an object. Also it always binds this...there are some scenarios where this arguably isn't wanted
Probably because other languages that have the same arrow function syntax (C#, Hack) don't have one. Yes yes, I know that CoffeeScript does.
That and the use cases are very narrow. There's not really a reason to complicate the syntax if it's not necessary. If you really want to maintain this, you can just use regular function syntax.
ESLint and TypeScript's compiler will complain about redeclaring functions. So, there really is no benefit whatsoever in practice. The const option is longer, less easy to scan, and you have to pseudo-declare your functions in the right order.
The only benefit is that it shows newbies that JS has first-class functions. I.e. that you can assign them to variables. Personally, I really don't care for that. First-class aren't that special anymore.
I'll always prefer the declarative option over an imperative one.
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.