In ECMAScript, the two most common methods of creating a function are function expressions and function declarations, the difference between the two is a bit dizzy, the specification of the ECMA is clear-the function declaration must be marked with an identifier (Identifier) is what everyone says (the function name), The function expression can omit the function name.
Then there is no function name that must be the function expression, then the function name, how to distinguish whether it is a function expression or function declaration ~ ~ ~ If it is part of an assignment expression is a function expression, if it is contained in a function body, or at the top of the program as a function declaration ~
function // declaration, because it is part of the program var function // expression, because it is part of an assignment expression New function // expression, because it is a new expression (function() { function// declaration, as it is part of the body of the function })();
Difference:
The difference between an expression and a declaration is very small-the declaration is preceded by any expression before parsing and calculating ~
Named function expression (function expression with function name)
Referring to a named function expression, it is a matter of course that it has to have a name, the preceding example var bar = function foo () {}; is a valid named function expression, but one thing to remember: This name only works within the scope of the newly defined function, Because the specification specifies that the identifier cannot be valid within the scope of the perimeter:
var f = function foo () {
Return typeof Foo; Foo is valid within the inner scope
};
Foo is used externally for non-visible
typeof Foo; "Undefined"
f (); "Function"
So what's the use of a named function expression, if so required? Why did you name it?
As we said at the beginning: to give it a name is to make the debugging process more convenient, because when debugging, if each item in the call stack has its own name to describe, then the debugging process is too cool, feel different.
For more information, please see the original page http://www.cnblogs.com/TomXu/archive/2011/12/29/2290308.html
function expressions and Function declarations ~ (own record)