Understanding of JavaScript scopes and block-level scopes, and understanding of javascript concepts
Scope is always the top priority of any programming language, because it controls the visibility and lifecycle of variables and parameters. Here, we first understand two concepts: block-level scope and function scope.
What is block-level scope?
The statement set in any pair of curly braces ({And}) belongs to a block. All variables defined in this block are invisible outside the block, which is called block-level scope.
The function scope is easy to understand (* ^__ ^ *). Parameters and variables defined in the function are invisible outside the function.
Most C classes have block-level scopes, but JS does not. See the following demo:
// C # include <stdio. h> void main () {int I = 2; I --; if (I) {int j = 3;} printf ("% d/n", j );}
When you run this code, the error "use an undefined variable: j" will occur. As you can see, the C language has a block-level scope. Because j is defined in the if statement block, it cannot be accessed outside the block.
But how does JS behave? Let's look at another demo:
functin test(){ for(var i=0;i<3;i++){ } alert(i); } test();
Run this code and "3" is displayed. It can be seen that the variable I defined in the block is still accessible outside the block. That is to say, JS does not support block-level scopes. It only supports function scopes, and variables defined at any position in a function are visible anywhere in the function.
So how can we make JS have block-level scope? Do you still remember that the variables defined in a function will be destroyed after the function is called? Can we use this feature to simulate the block-level scope of JS? See the DEMO below:
function test(){ (function (){ for(var i=0;i<4;i++){ } })(); alert(i); } test();
If you run it again, an "I" undefined error will pop up ~~~ Here, we place the for statement block in a closure, and then call this function. When the function call is complete, variable I is automatically destroyed, so we cannot access the block.
The closure feature of JS is the most important feature (* ^__ ^ *) that everyone understands ). In JS, to prevent name conflicts, we should avoid using global variables and global functions. So how can we avoid it? Yes, as shown in the demo above, we can put all the content to be defined into
(Function () {// content })();
At this time, do we add a function scope to their outer layers? Programs outside the scope cannot access them.
What does the block-level scope of javascript variables mean? Who can elaborate and understand?
In c and other languages. The above if (I) here the I scope is in the if Block
But in js. I is global. If so, if (var I = 0 ){}
Now you can access I in addition to the if block. This means there is no block Scope
How to Understand the scope chain in javascript?
The scope may not be so difficult to understand. The key is the coding specification. In the first echo (), because the name is defined in the function, the browser also defines the variable before using it during execution. I think the first echo () the browser interprets the Code as follows:
Var name = 'laruence ';
Function echo (){
Var name;
Alert (name );
Name = 'Eve ';
Alert (name );
Alert (age );
}
In this case, the first alert (name); is undefined. Remove the var name in the function and it will display the normal value. To avoid this problem, we recommend that you use different names or declare variables at the beginning of the function. We should write code according to appropriate coding specifications, rather than writing obscure or fuzzy code.