In some program -like programming languages, each code in the brackets has its own scope, and the variables are not visible outside the declaration of their code segment. We call the block scope (block scope) And JavaScript does not have a block -level scope. Instead of JavaScript, the function scope is used: variables are defined in the body of the function body and any function of the functional body nested by the variable. In the following code, I, J and K defined in different positions, they are defined in the same role in the same role
Copy code code as follows:
Function Text (O)
{{
var I = 0;
alert (typeof o);
if (typeof o == "string")
{{
var j = 0;
for (var k = 0; k <10; k ++)
{{
alert (k); // Output 0-9
}
alert (k); // Output 10
}
alert (j); // Output 0
}
JavaScript's function action domain refers to that all variables declared inside the function are always visible in the function body. Interestingly, this means that the variable is even available before declaration. The characteristics of JavaScript are informatively referred to as a statement, that is, all variables (not involved in the assignment) of the function declared by the function of JavaScript are "advanced" to the top of the function body. Look at the following code
Copy code code as follows:
var global = "globas";
Function Globals ()
{{
alert (global); // undefined
var global = "Hello QDao";
alert (global); // Hello QDAO
}
Due to the characteristics of the function scope, local variables are always defined in the entire function body, which means that the variables inside the function body cover the global variable of the same name. Although so when the program is executed to the VAR statement, the local variable will be truly associated. Therefore, the above process is equivalent to: the variables in the function are "advanced" to the top of the function body, and the initialization of the colleague variable stays at the original position. The
Copy code code as follows:
var global = "globas";
Function Globals ()
{{
var global;
alert (global); // undefined
global = "Hello QDAO";
alert (global); // Hello QDAO
}