A function is a first-class object in JavaScript, which means that a function can be passed like other values. A common usage is to pass an anonymous function as a callback function to an asynchronous function.
A function is a first-class object in JavaScript, which means that a function can be passed like other values. A common usage is to pass an anonymous function as a callback function to an asynchronous function.
Function Declaration
function foo() {}
The above method will be parsed (hoisted) before execution, so it exists in any part of the current context, even if it is called on the Function Definition body.
Foo (); // normal operation, because foo has been created before the code runs function foo (){
Function value assignment expression
var foo = function() {};
In this example, an anonymous function is assigned to the variable foo.
Foo; // 'undefined' foo (); // error: TypeErrorvar foo = function (){};
Var defines a declaration statement. the parsing of the variable foo is before the code runs. Therefore, the foo variable has been defined during code runtime.
However, since the value assignment statement is only executed at runtime, the foo value is undefined by default before the corresponding code is executed.
Value assignment expression of the namefunction
Another special case is to assign a name function to a variable.
Var foo = function bar () {bar (); // normal operation} bar (); // error: ReferenceError
Bar function declaration is not visible, because we have assigned the function to foo; however, it is still visible inside bar. This is because of the JavaScript naming process, and the function name is always visible in the function.
Note: In IE8 and IE8 or earlier versions, the browser bar is also visible externally because the browser has incorrectly parsed the name function assignment expression and parsed it into two functions: foo and bar.
The above is the JavaScript advanced series-function declaration and expression content. For more information, see the PHP Chinese website (www.php1.cn )!