JavaScript variable scope analysis _ javascript tips-js tutorial

Source: Internet
Author: User
The variable scope is the region in the program that defines this variable. First, paste a piece of code. If you are not confused about the output of the code, you do not need to read it below. The Code is as follows:


/* Code 1 */
Var scope = "global ";
Function checkScope (){
Var scope = "local ";
Function childCheck (){
Var scope = "childLocal ";
Document. write (scope );
}
Function childUndefined (){
Document. write (scope );
Var scope;
}
Function childOverride (){
Scope = "childOverride ";
Document. write (scope );
}
Document. write (scope); // output "local"
ChildCheck (); // output "childLocal"
ChildUndefined (); // output "undefined"
ChildOverride (); // output "childOverride"
Document. write (scope); // output "childOverride"
}
CheckScope (); // output "local childLocal undefinedchildOverride childOverride"
Document. write (scope); // output "global"


Global scope and local scope
The scope of global variables is global and is defined everywhere in Javascript. The variables declared in the function are local variables whose scope is local, it is defined only within the function body. The following output readers should not be surprised.

The Code is as follows:


/* Code 2 */
Var scope = "global ";
Function checkScope (){
Var scope = "local ";
Document. write (scope );
}
CheckScope (); // output "local"
Document. write (scope); // output "global"


You do not need to use the var statement when using a variable in the global variable scope. However, when declaring a local variable, you must use the var statement. Otherwise, it is considered as a reference to the global variable. See the following code:

The Code is as follows:


/* Code 3 */
Var scope = "global ";
Function checkScope (){
Scope = "local ";
Document. write (scope );
}
CheckScope (); // output "local"
Document. write (scope); // output "local"


No block Scope
Javascript has no block-level scope, and the variables declared in the function are defined throughout the function. The following code may surprise unfamiliar readers:

The Code is as follows:


/* Code 4 */
Var scope = "global ";
Function checkScope (){
Document. write (scope); // statement 4.1
Var scope = "local"; // statement 4.2
Document. write (scope );
}
CheckScope (); // output "undefinedlocal"


Because the variables declared in statement 4.1 (var scope = "local";) are valid throughout the scope of the checkScope function, in statement 4.2 (document. when write (scope);) is executed, scope references local variables. At this time, the local variable scope has not been defined, so the output is "undefined ". Therefore, a good programming habit is to put all the variable declarations together at the beginning of the function.

After learning about the above content, the reader should not be confused when looking at Code 1.

Object Property Variables
Attribute variables of an object are easy to understand. Readers should not be confused when looking at the code below the samples.

The Code is as follows:


/* Code 5 */
Var scope = "global ";
Var obj = new Object ();
Obj. scope = "object ";
Obj. checkScope = function (){
Var scope = "loacl ";
Document. write (scope); // output "loacl"
Document. write (this. scope); // output "object"
Document. write (window. scope); // output "global"
}
Obj. checkScope (); // output "loacl object global"

Related Article

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.