Function declaration and function expression in javascript, javascript expression
In fact, when the parser loads data to the execution environment, the function declaration is not the same as the function expression. The parser will first read the function declaration and make it available before executing any code, as for the function expression, you need to wait until the parser executes to its line of code to be actually executed.
Javascript: function declaration, function expression, and ()
Function name () {} function declaration,
Var name = function () {} function expression
Var name = function (){
}();
It does not conform to the above two types. It can only be said that none of them is true. However, you still need to make the final judgment based on the returned value of the anonymous self-running function on the right of the equal sign.
JavaScript: function declaration and function expression
Javascript is a language for interpreting execution, but it is also pre-compiled.
If (condition) {function sayHi () {alert ("Hi! ") ;}} Else {function sayHi () {alert (" Yo! ");}}
In this form, the JS engine registers the method to the window object during the pre-compilation process, that is, window. sayHi.
The if else condition is not taken into account, causing repeated sayHi methods to be registered. In this case, the syntax is invalid.
Var sayHi; if (condition) {sayHi = function sayHi () {alert ("Hi! ") ;};} Else {sayHi = function sayHi () {alert (" Yo! ");};}
The following is a variable (rather than a function), which can be assigned repeatedly. Therefore, the syntax is normal.
I wonder if I have made it clear.