The For loop in ECMAScript does not create a local context relative to C/A + +.
for (var in {a:1, B:2}) { // Although the Loop has ended but the variable k is still in the current scope
At any time, a variable can only be declared by using the var keyword.
The above assignment statement:
A = 10;
This simply creates 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 that it does not conform to the concept of a variable in the ECMAScript specification, so it is "not a variable" (it can be a property of a global object because there is a global object in JavaScript, Such an operation does not declare a variable but adds an a property to the global object.
Let's look at a simple example to illustrate the problem.
if inch window)) { var a = 1;} alert (a);
First, all global variables are properties of the window, and the statement var a = 1; equivalent to WINDOW.A = 1;
You can detect if a global variable is declared in the following way
in Window
Second, all the variable declarations are at the top of the range scope and look at similar examples:
inch window); var A;
At this point, although the declaration is after alert, the alert pops up is still true, because the JavaScript engine first graves all the variable declarations and then moves the variable declarations to the top, and the final code effect is this:
var A;alert ( in window);
Third, you need to understand that the meaning of the topic is that 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:
var A; // statement a = 1; // Initialize Assignment
So the summary is that when the variable declaration and assignment are used together, the JavaScript engine automatically divides it into two parts in order to advance the variable declaration, not to advance the assignment because he has the potential to influence the code to perform unexpected results.
The code in the title is equivalent to:
var A; if inch window)) { = 1;} alert (a);
According to the above example analysis, declare the variable if it is declared in front of the local variable must be added Var, if the declaration is the global variable can not add var (it is better to limit the number of global variables, try to use local variables)
Here are a few features that use Var
Declaring a variable multiple times with the Var statement is not only legal, but it does not cause any errors. If a repeating declaration has an initial value, it assumes the role of an assignment statement. If a duplicate declaration does not have an initial value, it does not have any effect on the existing variable.
Variables that do not have a VAR declaration are present as global variables, and variables declared with VAR are local variables, especially inside the function. And, after testing, the Var declaration is faster than without the Var. As much as possible in the function of local variables, so that is safe and fast, variable operation is more reasonable, not because the function of the random operation of global variables resulting in a logic error.
When declaring an object, it is best to use the object's self-polygon amount, which is much faster than the new method.
Variable names are taken by themselves, and in order to take care of semantics and specifications, variable names may be slightly longer, but note that the length of variable names can also affect the speed of code execution. Long variable name declarations do not perform as quickly as they are short.
A little comment on the JavaScript variable declaration