Home > Web Front-end > JS Tutorial > body text

Authoritative Guide to JavaScript Study Notes Sharing Variable Scope_Javascript Skills

WBOY
Release: 2016-05-16 18:01:40
Original
1030 people have browsed it

I don’t know how you understand the “declaration” and “definition” of variables in the language.
My understanding is as follows:
“Declaring” a variable means just declaring it, while “defining” a variable means declaring it. , and assigned a value.
For example:

Copy code The code is as follows:

var name;//Just a statement
var num = 11;//Declaration and assignment, that is, it is defined
var password = "yangjiang";//Declaration and assignment, that is, it is defined

The following are a few Point summary:
Scope of variables: global and local. (Note: If you try to read the value of an undeclared variable, JavaScript will generate an error)
First point: In the case of using the var keyword to modify variables, if a local variable or function parameter is declared The name is the same as the name of a global variable,
then the global variable is effectively hidden.
For example:
Copy code The code is as follows:

var scope1 = "global"; //var modification
function checksScope(){
var scope1 = "local";//var modification
document.write(scope1);
}checksScope();//local

Second point: If you try to give a variable declared without the var keyword, then the implicitly declared variable is always created as a global variable, even if
the variable is only used within a function body (It will only take effect if the function is run.) Note that function nesting is not supported.
For example:
Copy code The code is as follows:

scope2 = "globalAAAAAA";/ / No var modification is used (js will declare it as a global variable by default)
function checkScopeA(){
scope2 = "localAAAAA"; // No var modification is used (js will declare it as a global variable by default)
document.write("
" scope2);
myscope = "myLocalAAAAA";//No var modification is used (js will declare it as a global variable by default)
document.write ("," myscope);
}
checkScopeA();//localAAAAA, myLocalAAAA *A
document.write("
" scope2);//localAAAAA *B
document.write("
" myscope);//myLocalAAAAA *C

If you comment out the code at *A in the above example,
For example:
Copy code The code is as follows:

scope2 = "globalAAAAA";//No var modification is used (js will declare it as a global variable by default)
function checkScopeA(){
scope2 = "localAAAAA";//No var modification is used (js will declare it as a global variable by default)
document. write("
" scope2);
myscope = "myLocalAAAAA";//No var modification is used (js will declare it as a global variable by default)
document.write("," myscope );
}
//checkScopeA(); *A
document.write("
" scope2);//globalAAAAA *B
document.write("< br/>" myscope);//An error occurred *C

Because the function checkScopeA is not executed, the output at *B is globalAAAAA;
Because the function checkScopeA is not executed, the variable myscope is not Declaration, if you try to read an undeclared variable, an error will occur.
The third point:
In JavaScript, function definitions can be nested. Since each function has its own local scope, it is possible to have several nested levels of local scopes.
For example:
Copy code The code is as follows:

var scope3 = "global scope" ; //A global variable is defined
function checkScopeB(){
var scope3 = "local scope"; //A local variable is defined, overriding the global variable scope3
function nested(){
var scope3 = "nested scope"; //A local variable is defined inside the function of the function
document.write("
" scope3); //nested scope
}
nested();
}
checkScopeB();//nested scope

The fourth point:
In javascript, there is no block-level scope, it is declared in the function All variables, no matter where they are declared, are declared throughout the function.
In JavaScript, there is no block-level scope. All variables defined in a function, no matter where they are defined, are defined throughout the function.
For example:
Copy code The code is as follows:

function test(o){//According to the above description: the scopes of the three variables i, j, and k in this function are the same.
var i = 0; //Variable i is defined throughout the function
if(typeof o == "object"){
var j = 0; //Variable j is defined throughout the function Defined, not just in the if statement block
for(var k=0;k<10;k){//The 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 at this time is still defined, k=10
}
document.write("
value of j:" j); //Variable j has been declared, but it may It has not been initialized because the parameters passed into the function may not be objects, and the if statement block will not be executed
}

This function is called in two ways:
Method 1: Pass Input object
test({});//Output result: Comment in the above example
Method 2: Pass nothing
test();//Output result: value of j: undefined
What I don’t understand is why the output result in the second method is undefined. What I guessed at the time was: The value of j: 0
Later, this book said:
Since local variables are declared (or defined) in the entire function body, this means that in the entire function body The global
variable with the same name is hidden in . Although local variables are declared (or defined) throughout the function body, they will not be initialized before the var statement is executed.
In this case, the output result of the call in the second method above is easier to explain. Since the variable j is defined in the entire function, and since the parameters passed into the function are empty, the if statement in the function body will not be executed, thus making the value of j undefined. (This is my understanding based on the sentence in the book above)
The following example is a better explanation:
Copy code The code is as follows:

var sssss = "Global variable";
function f(){
document.write ("
" sssss);//Output: undefined instead of outputting "global variables"
var sssss = "local variables";
document.write("
" sssss);//Output: local variable
}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!