What is the difference between
var a = function() {}
and
var a = function b() {}
The latter, b is undefined?
What is the difference between
var a = function() {}
and
var a = function b() {}
The latter, b is undefined?
The second one is a named anonymous function - the name will appear in a stacktrace (otherwise in the stacktrace you''ll see just "anonymous function")
The first is an anonymous function expression and the second a named function expression, both valid in Javascript.
For example, it can be used for recursion without arguments.callee
(deprecated and not permitted in strict mode), because it refers to itself, no matter where. The scope of the reference is local only to inside the function, that is it isn't accessible globally:
var a = function b(){
return b;
};
function c() {
return c;
}
var d = function e() {
return e();
};
d(); // maximum call stack size exceeded :P
var f = c;
c = null;
f(); // null
a(); // function
b();// undefined not a function
b; // not defined
a()(); // same function again
var a = function() {}
Function name can be omitted. In this case function name is omitted. These functions are called anonymous functions.
Read about javascript scope and anonymous function advantages and disadvantages for details.