This article is a summary of some of my views, thoughts, and understandings on variable declarations in javascript, we hope to help our friends. Compared with C/C ++, The for loop in ECMAScript cannot create a local context.
The Code is as follows:
For (var k in {a: 1, B: 2 }){
Alert (k );
}
Alert (k); // although the loop has ended, the variable k is still in the current scope
Variables can only be declared by using the var keyword at any time.
The above assignment statement:
A = 10;
This only creates a new attribute for the Global Object (but it is not a variable ). "Not a variable" does not mean that it cannot be changed, but does not comply with the variable concept in the ECMAScript specification, so it is "not a variable" (the reason why it can become a global object attribute, this is because javascript has a global object. This operation does not declare a variable, but adds a property to the global object.
The following is a simple example to illustrate the problem.
The Code is as follows:
If (! ("A" in window )){
Var a = 1;
}
Alert ();
First, all global variables are the properties of window. The statement var a = 1 is equivalent to window. a = 1;
You can use the following method to check whether global variables are declared.
"Variable name" in window
Second, all variable declarations are at the top of the scope. Let's take a look at similar examples:
The Code is as follows:
Alert ("a" in window );
Var;
At this point, although the Declaration is after alert, the alert pop-up is still true, because the JavaScript engine will first scan all the variable declarations and then move these variable declarations to the top, the final code effect is as follows:
The Code is as follows:
Var;
Alert ("a" in window );
Third, you need to understand the meaning of this question: the variable declaration is advanced, but the variable assignment is not, because this line of code includes the variable declaration and variable assignment.
You can split the statement into the following code:
The Code is as follows:
Var a; // Declaration
A = 1; // initialization value assignment
In summary, when the variable declaration and value assignment are used together, the JavaScript engine automatically divides it into two parts to declare the variable in advance, the procedure of not assigning values in advance is because it may affect the code execution to produce unexpected results.
The code in the question is equivalent:
The Code is as follows:
Var;
If (! ("A" in window )){
A = 1;
}
Alert ();
According to the analysis in the above example, when declaring a variable, if it is a declared local variable, we must add var before it. If it is declared a global variable, we can not add var (it is best to limit the number of global variables, use local variables whenever possible)
The following describes several features of using var.
Using the var statement multiple times to declare a variable is not only legal, but also does not cause any errors.
If a declaration used repeatedly has an initial value, it assumes only the role of a value assignment statement.
If a declaration used repeatedly does not have an initial value, it will not have any impact on the original variables.
Variables without var declaration exist as global variables. Variables With var declaration belong to local variables, especially inside functions. In addition, after testing, the speed with var declaration is faster than without var. Set as many local variables as possible in the function. This is safe and fast, and variable operations are more reasonable. It does not cause logical errors due to the random operations on global variables in the function.
When declaring an object, it is best to use the object's self-plane volume method, which is much faster than the new method.
Variable names are obtained by yourself. To take care of the semantics and norms, variable names may be slightly longer. However, note that the length of variable names also affects the code execution speed. The execution speed of long variable name declaration is not short and fast.