I have been reading the fifth edition of the javascript authoritative guide recently. The variable scope chapter is really tired. However, there are still many gains. I don't know, how do you understand the "Declaration" and "Definition" of variables in languages,
My understanding is as follows:
The "Declaration" variable is just declaration, and the "Definition" variable refers to Declaration and value assignment.
For example:
The Code is as follows:
Var name; // just declare
Var num = 11; // declare and assign values, that is, define
Var password = "yangjiang"; // declare and assign values.
The following is a summary:
Variable scope: global and local. (Note: javascript generates an error if you try to read the value of an undeclared variable)
First: When the var keyword is used to modify a variable, if the name declared for a local variable or function is the same as the name of a global variable,
The global variable is effectively hidden.
For example:
The Code is as follows:
Var scope1 = "global"; // var Modifier
Function checksScope (){
Var scope1 = "local"; // var Modifier
Document. write (scope1 );
} ChecksScope (); // local
Second, if you try to give a variable that is not declared using the var keyword, the variables that are implicitly declared are always created as global variables, even if
This variable is only used in a function (only when the function runs). Note that function Nesting is not supported.
For example:
The Code is as follows:
Scope2 = "globalAAAAA"; // var is not used (js will declare it as a global variable by default)
Function checkScopeA (){
Scope2 = "localAAAAA"; // var is not used (js declares it as a global variable by default)
Document. write ("
"+ Scope2 );
Myscope = "myLocalAAAAA"; // var is not used (js will declare it as a global variable by default)
Document. write ("," + myscope );
}
CheckScopeA (); // localAAAAA, myLocalAAAAA *
Document. write ("
"+ Scope2); // localAAAAA * B
Document. write ("
"+ Myscope); // myLocalAAAAA * C
If you comment out the code at * A in the above example,
For example:
The Code is as follows:
Scope2 = "globalAAAAA"; // var is not used (js will declare it as a global variable by default)
Function checkScopeA (){
Scope2 = "localAAAAA"; // var is not used (js declares it as a global variable by default)
Document. write ("
"+ Scope2 );
Myscope = "myLocalAAAAA"; // var is not used (js will declare it as a global variable by default)
Document. write ("," + myscope );
}
// CheckScopeA (); *
Document. write ("
"+ Scope2); // globalAAAAA * B
Document. write ("
"+ Myscope); // Error * C
Because the checkScopeA function is not executed, * B Outputs globalAAAAA;
Because the checkScopeA function is not executed, the variable myscope is not declared. If you try to read an undeclared variable, an error will occur.
Third point:
In javascript, function definitions can be nested. Since each function has its own local scope, several nested layers with local scopes may appear.
For example:
The Code is as follows:
Var scope3 = "global scope"; // defines a global variable.
Function checkScopeB (){
Var scope3 = "local scope"; // defines a local variable that overwrites the global variable scope3.
Function nested (){
Var scope3 = "nested scope"; // a local variable is defined within the function.
Document. write ("
"+ Scope3); // nested scope
}
Nested ();
}
CheckScopeB (); // nested scope
Point 4:
In javascript, there is no block-level scope. All variables declared in the function are declared no matter where they are declared.
In javascript, there is no block-level scope. All variables defined in the function are defined in the entire function, no matter where they are defined.
For example:
The Code is as follows:
Function test (o) {// according to the preceding description: The I, j, and k variables in this function have the same scope.
Var I = 0; // variable I is defined in the entire function.
If (typeof o = "object "){
Var j = 0; // variable j is defined in the entire function, not just in the if statement block.
For (var k = 0; k <10; k ++) {// variable k is defined in the entire function, not just in the if statement Block
Document. write ("
The value of k is: "+ k );
}
Document. write ("
The value of k outside the for Loop: "+ k); // k is still defined at this time, k = 10
}
Document. write ("
J value: "+ j); // variable j is declared, but may not be initialized because the parameter passed in to the function is not an object and if statement block will not be executed
}
The following two methods call this function:
Method 1: import an object
Test ({}); // output result: comment in the previous example
Method 2: Do not upload anything
Test (); // output result: Value of j: undefined
What I cannot understand is why the output result in the second method is undefined. I guess: j value: 0
Later, the book said:
Because local variables are declared (or defined) in the entire function body, this means that the global variables with the same name are hidden in the entire function body.
Variable. Although local variables are declared (or defined) in the entire function, they are not initialized before the var statement is executed.
In this case, the output result of the preceding method 2 call is better explained, because the variable j is defined in the entire function, and because the input function parameter is empty, therefore, the if statement in the function body is not executed, so that the value of j is undefined. (This is my understanding of the sentence mentioned above)
The following example provides a better description:
The Code is as follows:
Var sssss = "global variables ";
Function f (){
Document. write ("
"+ Sssss); // output: undefined instead of" global variable"
Var sssss = "local variable ";
Document. write ("
"+ Sssss); // output: local variable
}