This article mainly introduces the knowledge of immediate function execution in js, which has good reference value. Let's take a look at it with the small series below. 1. anonymous functions cannot be defined separately. They must be assigned values or executed immediately. Otherwise, they will be defined as syntax errors by the JS engine.
Function () {alert (dada);} VM229: 1 Uncaught SyntaxError: Unexpected token
2. You can call the function immediately after adding brackets to the function body. The function form must be a function expression, not a function declaration.
Function () {alert (123) ;}(); VM265: 1 Uncaught SyntaxError: Unexpected token
3. You can add a symbol before the function, or wrap the function in brackets to eliminate the function declaration.
(Function () {alert (123) ;}) (); undefined
4. The safest way to eliminate the function declaration is to add parentheses, because the operator number is also calculated with the return value of the function, causing unnecessary trouble.
5. The brackets of the function expression can be enclosed or not. The effect is the same.
(Function () {alert (123) ;}(); undefined
6. Immediate function execution: Creates a scope space to prevent variable conflicts or overwrites.
For more articles about immediate function execution in JS, please follow the PHP Chinese website!