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.
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.
Callbacks, promise resolution, lambdas. Anywhere you're writing anonymous functions as a parameter to another function. Obviously it doesn't matter if you don't write much functional code, but I tend to.
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
Edit: and anyways if you don't want to bind this, don't use an arrow function
I can see many cases for that. Say you work in an instance of an object and would want to use multiple functions inside your class for your code, it doesn't work with arrow functions properly (or you set the this variable to something else). And i would separate those functions to make my functions lean and clean.
That would mean that if i would use arrow functions for other items, it gets mixed and makes it into more spaghetti than i would normally have when i'm not using them.
I can understand using them to minify your code but for reading and working with it, it doesn't hold any benefit whatsoever.
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.
Honestly the main thing that annoys me about it is it makes the language totally inconsistent. If you want this binding you use () => but if you don't you use function()? Those are completely different. It makes me feel like I am using some patchwork language (which I am).
19
u/delventhalz Apr 01 '17
To be honest, I wouldn't mind being able to use
fnorfunc.