Compared with C/C++, the for loop in ECMAScript cannot create a local context.
The code copy is as follows:
for (var k in {a: 1, b: 2}) {
alert(k);
}
alert(k); // Even though the loop has ended, the variable k is still in the current scope
At any time, variables can only be declared by using the var keyword.
The assignment statement above:
a = 10;
This is simply creating a new property for the global object (but it is not a variable). "Not a variable" does not mean that it cannot be changed, but means that it does not conform to the variable concept in the ECMAScript specification, so it is "not a variable" (the reason why it can become a property of a global object is entirely because there is a global object in JavaScript. Such an operation is not to declare a variable but to add an a property to the global object.
Let's see a simple example to illustrate the problem
The code copy is as follows:
if (!("a" in window)) {
var a = 1;
}
alert(a);
First of all, all global variables are properties of window, and the statement var a = 1; is equivalent to window.a = 1;
You can use the following method to detect whether the global variable is declared
"Variable Name" in window
Second, all variable declarations are at the top of the scope scope, look at a similar example:
The code copy is as follows:
alert("a" in window);
var a;
At this time, although the declaration is after the alert, the alert pops up still true, because the JavaScript engine will first sweep all variable declarations, and then move these variable declarations to the top. The final code effect is as follows:
The code copy is as follows:
var a;
alert("a" in window);
Third, you need to understand the meaning of the question is that the variable declaration is advanced, but the variable assignment does not, because this line of code includes the variable declaration and variable assignment.
You can split the statement into the following code:
The code copy is as follows:
var a; // Statement
a = 1; //Initialize the assignment
So to sum up, when variable declaration and assignment are used together, the JavaScript engine will automatically divide it into two parts to advance the variable declaration. The step of assignment is not advance because it may affect the code execution and achieve unexpected results.
The code in the question is equivalent to:
The code copy is as follows:
var a;
if (!("a" in window)) {
a = 1;
}
alert(a);
According to the analysis of the above example, if the variable is declared, you must add var before the declared local variable. If the declared global variable, you can not add var (it is best to limit the number of global variables and try to use local variables)
The following describes several features of using var
Using a var statement to declare a variable multiple times is not only legal, but also does not cause any errors.
If a reused declaration has an initial value, it only serves as an assignment statement.
If a reused declaration does not have an initial value, it will not have any effect on the original variable.
Variables without var declarations exist as global variables; variables with var declarations belong to local variables, especially inside functions. And, after testing, it is faster to declare with var than to not with var. Set as many local variables as possible in the function, which is safe and fast, and variable operations are more reasonable, and there will be no logical errors caused by random operation of global variables in the function.
It is best to use the object's own face-to-face method when declaring an object, as this speed is much faster than the new method.
The variable name is taken by yourself. In order to take care of semantics and specifications, the variable name may be slightly longer, but be careful, the length of the variable name will also affect the execution speed of the code. Long variable name declarations are not as fast as short.