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"