The first is a function declaration. It rises (you can use it anywhere inside the current volume).
The second option is to define a variable using an anonymous function. The variable rises, the destination remains in place. The function cannot be used before the line where you assigned it.
The third one assigns a global method. Like the second, although it works with a global object, which is not very good.
However, you might think of a fourth alternative (named functional expression):
var foo = function bar(){
Here, the bar will be accessible only within itself, which is useful for recursion, and not for changing the current area.
You choose any approach based on your needs. I would vote against the second approach, as it behaves like a variable.
As soon as you indicate both the second and the third options, I would like to recall that a polluting global object is considered bad practice . You better think about using spontaneous anonymous functions to create a separate area, for example.
(function(){ var t = 42;
I suppose you can find a more detailed article on JavaScript Scoping and Hoisting .
source share