I do not know how to understand the "declaration" and "definition" of variables in the language,
My understanding is as follows:
A "declaration" variable is simply a declaration, and a "definition" variable, which is declared and assigned.
For example:
Copy Code code as follows:
var name;//just declares
var num = 11;//Declaration and assignment, which defines the value of the
var password = "Yangjiang";/Declaration and assignment, which defines the value of the
Here are a few things to summarize:
The scope of the variable: global and Local. (Note: If you try to read the value of an undeclared variable, JavaScript generates an error)
1th: If you use the var keyword to modify a variable, the name of a local variable or function is declared with the same name as a global variable.
Then it effectively hides the global variable.
For example:
Copy Code code as follows:
var scope1 = "global";//var modification
function Checksscope () {
var scope1 = "local";//var decoration
document.write (SCOPE1);
}checksscope ();//local
2nd: If you try to give a variable that is not declared with the Var keyword, the implicitly declared variable is always created as a global variable, even if
The variable is only used in one function (only if the function is running), and it does not support the case that the function is nested.
For example:
Copy Code code as follows:
Scope2 = "GLOBALAAAAA";//not using the Var modifier (JS will default to declare it as a global variable)
function Checkscopea () {
Scope2 = "LOCALAAAAA";//not using the Var modifier (JS will default to declare it as a global variable)
document.write ("<br/>" +scope2);
Myscope = "MYLOCALAAAAA";//not using the Var modifier (JS will default to declare it as a global variable)
document.write ("," +myscope);
}
Checkscopea ();//LOCALAAAAA,MYLOCALAAAAA *a
document.write ("<br/>" +scope2);//localaaaaa *b
document.write ("<br/>" +myscope);//mylocalaaaaa *c
If you comment out the code at the *a in the example above,
For example:
Copy Code code as follows:
Scope2 = "GLOBALAAAAA";//not using the Var modifier (JS will default to declare it as a global variable)
function Checkscopea () {
Scope2 = "LOCALAAAAA";//not using the Var modifier (JS will default to declare it as a global variable)
document.write ("<br/>" +scope2);
Myscope = "MYLOCALAAAAA";//not using the Var modifier (JS will default to declare it as a global variable)
document.write ("," +myscope);
}
Checkscopea (); *a
document.write ("<br/>" +scope2);//globalaaaaa *b
document.write ("<br/>" +myscope);//error occurred *c
Because the function Checkscopea is not executed, the *b output is globalaaaaa;
Because the function Checkscopea is not executed, the variable myscope is not declared and an error occurs if you attempt to read an undeclared variable.
3rd:
In JavaScript, the definition of a function can be nested. Because each function has its own local scope, it is possible to have several nested layers of local scopes.
For example:
Copy Code code as follows:
var scope3 = "global scope"; Defines a global variable
function Checkscopeb () {
var scope3 = "local scope"; Defines a local variable that overrides the global variable Scope3
function nested () {
var scope3 = "Nested Scope"; Within the function's function, a local variable is defined
document.write ("<br/>" +scope3); Nested scope
}
Nested ();
}
Checkscopeb ();//nested scope
4th:
In JavaScript, there is no block-level scope, and all variables declared in a function, regardless of where they are declared, are declared throughout the function.
In JavaScript, there is no block-level scope, and all the variables defined in the function, regardless of where they are defined, are defined throughout the function.
For example:
Copy Code code as follows:
function Test (o) {//According to the description above: The scope of the I,j,k three variables in this function is the same.
var i = 0; The variable i is defined throughout the function
if (typeof o = = "Object") {
var j = 0; Variable j is defined throughout the function, not just in the IF statement block
for (Var k=0;k<10;k++) {//variable k is defined throughout the function, not just in the IF statement block
document.write ("<br/>k value is:" +k);
}
document.write ("The value of K in the <br/>for loop:" +k);//k at this time is still defined, k=10
}
document.write ("<br/>j value:" +j); The variable j is declared, but may not be initialized because the parameter passed in to the function is not an object and the IF statement block does not execute
}
The following call this function in two ways:
Way One: Incoming objects
Test ({});//Output Result: Comments in the example above
Mode two: Do not preach anything
Test ();//Output Result: Value of J: undefined
What I don't understand is that the output in the second way is undefined. I was guessing: the value of J: 0
Later, the book says:
Because local variables are declared (or defined) throughout the function, this means that the global of the same name is hidden throughout the function body
Variable. Although local variables are declared (or defined) throughout the function body, they are not initialized until the VAR statement is executed.
In this case, the above way the output result of the two calls is better explained, because the variable j is defined throughout the function, and because the arguments to the function are NULL, the IF statement in the function body does not execute so that the value of J is undefined. (This is my understanding of the words in the book above)
Here's an example of a better description:
Copy Code code as follows:
var sssss = "global variable";
function f () {
document.write ("<br/>" +sssss);//output: Undefined instead of output "global variables"
var sssss = "local variable";
document.write ("<br/>" +sssss);//output: local variable
}