Lexical scopes of JavaScript

Source: Internet
Author: User
In & lt; JavaScript authoritative guide & gt;, the description of JS lexical scopes is: Functions in JavaScript divide scopes Through lexical rather than dynamically divide scopes. this means that they run in the scope that defines them, rather than in the scope that executes them. it seems like... syntaxHighlighter

The description of the JS lexical scope is as follows:

Functions in JavaScript divide scopes by lexical instead of dynamically dividing scopes. This means that they run in the scopes that define them, rather than in the scopes that execute them.

At last, it seems to be clear, but it seems a bit vague to think about it.

Let's use an example to show it.


[Javascript]
Var I = 1;
Function (){
Alert (I); // 1
}
A ();
Var I = 1;
Function (){
Alert (I); // 1
}
A ();

Above, variable I is a global variable, and its scope is the entire js file.

In fact, we can regard the above Code as a global function, as follows:


[Javascript]
(Function (){
Var I = 1;
Function (){
Alert (I); // 1
}
A ();
})();
(Function (){
Var I = 1;
Function (){
Alert (I); // 1
}
A ();
})();

 


This does not reflect the special features of JS lexical scopes;

 


Let's take a look at the following code:

[Javascript]
(Function (){
Var I = 1;
Function (){
Alert (I); // undefined
Var I = 2;
Alert (I); // 2
}
A ();
})();
(Function (){
Var I = 1;
Function (){
Alert (I); // undefined
Var I = 2;
Alert (I); // 2
}
A ();
}) (); Unexpectedly, the first pop-up warning was not 1, but undefined.
This is obviously incredible.

 


In fact, this is the so-called: they run in the scope that defines them, rather than in the scope that executes them.

Because var I = 2 is defined when function a () is defined, the scope of variable I called in function a () is limited to the one in function.

When the first alert (I) is triggered when a function is called, the variable I is not defined, so the undefined warning is displayed,

When the second alert (I) is triggered, variable I has been assigned a value of 2.

 

 

The code is actually equivalent to the following:


[Javascript]
(Function (){
Var I = 1;
Function (){
Var I;
Alert (I );
I = 2;
Alert (I );
}
A ();
})();
(Function (){
Var I = 1;
Function (){
Var I;
Alert (I );
I = 2;
Alert (I );
}
A ();
})();

 

This is the so-called: when defining a function, the scope of the variable has been divided.

Excerpt from jian sheng's code Memorandum
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.