JavaScript variable Scope Analysis _javascript techniques

Source: Internet
Author: User
Tags variable scope
Copy Code code 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
Global variables are global in scope and are defined everywhere in JavaScript, and variables declared within functions are local variables that are local in scope and are defined only within the body of the function. For the following output readers should not be surprised.
Copy Code code as follows:

/* Code 2 * *
var scope = "global";
function Checkscope () {
var scope = "local";
document.write (scope);
}
Checkscope (); Output "local"
document.write (scope); Output "global"

Variables can be used in global variable scopes without the Var statement, but if the local variable is declared to be certain to use the Var statement, it is considered a reference to the global variable. Look at the following code:
Copy Code code 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 does not have a block-level scope, and the variables declared in the function are defined throughout the function. The following code may be quite unexpected to unfamiliar readers:
Copy Code code 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 statement 4.1 (var scope = "local";) the variable declared is valid throughout the Checkscope function scope, the statement 4.2 (document.write (scope); A local variable is referenced at the time of execution, and the local variable scope is not defined, so output "undefined". So a good programming habit is to put all the variable declarations together and place them at the beginning of the function.

Once you know the above, it's not confusing for the reader to look at code 1 again.

object's Property variable
The object's property variable is easier to understand, so look at the code below and the reader should not be confused.
Copy Code code 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.