In C/C ++, all code blocks enclosed by curly brackets have their own scopes, that is, block-level scopes (Private scopes ). In JavaScript, there is no block-level scope. First, let's look at a piece of code:
For (VAR I = 0; I <10; I ++ ){
}
Alert (I );
The execution result is:
10
For languages with block-level scopes, variable I defined and initialized in the for statement cannot be accessed outside the loop, while in Javascript, variable I defined in the for statement will still exist in the execution Environment (SCOPE) outside the loop after the loop ends. Here, the scope of I is the global environment. Specifically, when the VaR keyword is used to declare a variable, the variable is automatically added to the nearest available environment. For a function, this recent environment is the local environment of the function. If the variable is initialized without declaration, it is automatically added to the global environment.
However, sometimes block-level scopes are required to solve some problems. In this case, we can use anonymous functions to simulate block-level scopes.
Anonymous FunctionsIt is a function without a name. It is sometimes called a Lambda function. The format is as follows:
Function functionname (arg0, arg1 ){
// Function body
}
Used as an imitationBlock-level scope(Private scope) the syntax of anonymous functions is as follows:
(Function (){
// Block-level scope
})();
The preceding Code defines and calls an anonymous function immediately. Include the function declaration in parentheses, indicating that it is actually a function expression. The other pair of parentheses that follow it indicates that this function is called immediately.
At the beginning, we may feel that this syntax is hard to understand. Let's take a look at the following code:
VaR myfunc = function (){
Alert ('function ');
};
Myfunc ();
In the code above, a function is defined in the form of a function expression, and then called immediately. To define a function, create an anonymous function and assign it to the variable myfunc. Then, add a pair of parentheses after the function name to call the function. If we directly use an anonymous function to represent the variable myfunc and add a pair of parentheses after the anonymous function, doesn't it mean that it is called directly? However, if you do this as follows, an error will be reported:
Function (){
// Block-level scope
}();
In JavaScript, the function keyword indicates the beginning of a function declaration, and the function declaration cannot be followed by parentheses directly. Function expressions can be followed by parentheses to indicate function calls. Adding a pair of parentheses to the function declaration can be converted into a function expression, as shown below:
(Function (){
// Block-level scope
})();
In this way, the simplest block-level scope is created. This technique is used outside the function in the global scope to restrict adding too many variables and functions to the global scope. For example:
(Function (){
VaR now = new date ();
If (now. getmonth () = 0 & now. getdate () = 1 ){
Alert ("Happy New Year ");
}
})();
The above code is put in the global scope to confirm that a message to congratulate the new year will be displayed on April 9, January 1. Here, the variable now is the local variable in the block-level scope imitated by the anonymous function.
Of course, as long as we need some variables temporarily, we can use block-level scopes (Private scopes ). When an anonymous function is executed, its scope chain is immediately destroyed, which can reduce the resource occupation of the closure.