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.
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.
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.
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.
No, because functions are hoisted. It also makes it clear what you export at the beginning of the module so you can quickly discern between what's "private" and what's "public".
It's documentation on the matter (emphasis added):
functions (boolean) - The flag which shows whether or not this rule checks function declarations. If this is true, this rule warns every reference to a function before the function declaration. Otherwise, ignores those references. Function declarations are hoisted, so it’s safe. Default is true.
3
u/Disgruntled__Goat Apr 01 '17
Why? I was under the impression no one uses
functionat all now.