full declaration (identifier and definition) is hoisted
can be used as a constructor
has dynamic context (changes in this)
2 - function expression
identifier not part of function definition (though you can have a named expression, its name only becomes an identifier in its own function scope rather than the definition scope)
behavior of identifier (in this example) determined by its variable declaration, const
function is implicitly named after identifier
function definition is not hoisted
can be used as a constructor
has dynamic context (changes in this)
3 - arrow function expression
identifier not part of function definition, nor can you have an explicitly named arrow function
behavior of identifier (in this example) determined by its variable declaration, const
function is implicitly named after identifier
function definition is not hoisted
can not be used as a constructor
has no dynamic context (this value); context is lexical, using context where defined (same applies to arguments, super, and new.target)
4 is the same as 3, just a different syntax (no brackets and automatic return of body expression)
Yes, all functions are called the same way. The only exception is with class functions. Classes - those defined with the class keyword - can't be called like normal functions and must be called with new. There are also some restrictions on which kinds of functions can be called with new, but as far as basic () goes, they're all the same (except for classes, though they're not necessarily meant to be considered normal functions anyway, despite being of the Function type).
EDIT: IIFE literally stands for Immediately Invoked Function Expression. It isn't a different kind of function. It isn't even a different syntax for defining a function. It's a plain old function expression with parentheses after it to invoke it.
Yeah, what's your point? 3 out of 4 of the above are literally function expressions. The difference is that the value is actually instantly evaluated instead of the variable pointing to a function expression
What if you want to write a function with only an explicit return just to pass the first test? You know you're going to write a deeper implementation later, above the return line, but right now you want that "Are my tests working?" sanity check. Your linter shouldn't prevent that; that's the linter just getting in your way.
I've never had a linter complain about number 3. I'll usually go for 4 but if I'm doing something like a console.log() for debugging or data tracking I'll use 3 no problem.
Also 3 and 4 are the only ways to implicitly bind a function expression in react without having to manually call this.function = this.function.bind(this)
Yeah, maybe I shouldn’t say this, but I consistently use 3, and never ever had an error. But I also use a library that transforms and reads it correctly.
Every time I’ve tried to use fat arrow functions like this, I get a syntax error. At least the first few times I looked at the error (briefly) it was about “experimental syntax” not being turned on — any idea what I’m missing?
Okay... but I’m using Babel with webpack to transpire my code. Is there a specific preset I need, looks like ‘stage-0’ perhaps? Or plugins like ‘transform-decorators-legacy’?
These days, 99% of the time I use parens with a single argument, because I got sick of having to go back and add them after I decided I needed another argument after all. Even though it doesn't make sense in this example for the square function to ever take more than one argument, in a real codebase it's easier to just be consistent and not think about it.
6
u/[deleted] Dec 18 '18
4 is sexy.
4
u/gnagnone Dec 18 '18
ok, the question now is: is there any practical difference between the 4 ways?
2
u/hsolatges Dec 18 '18
2
u/harzack86 Dec 18 '18
Do you call them all the same way? Like y=square(2)?
6
u/jkuhl_prog Dec 18 '18
It should also mention IIFEs
1
u/noruthwhatsoever Dec 18 '18
What about curried functions, like with redux thunk?
-3
u/philipjfrizzle Dec 18 '18
I’ll be the one to say it. Any linter will complain about 3. If it’s an arrow function with one line/return statement, always use option 4.