In depth understanding of javascript variable declarations

  • 2020-03-30 04:21:07
  • OfStack

In contrast to C/C++, the for loop in ECMAScript does not create a local context.


for (var k in {a: 1, b: 2}) {
  alert(k);
}

Alert (k); // the variable k is still in scope even though the loop has ended
Variables can only be declared at any time by using the var keyword.
 
The assignment statement above:
 
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 becomes a property of a global object only because there is a global object in javascript, and such an operation does not declare a variable but adds an a property to the global object.
 
Now let's do a simple example to illustrate this


if (!("a" in window)) {
    var a = 1;
}
alert(a);

First, all global variables are properties of window. Var a = 1; This is the same thing as window.a = 1;
 
You can check if a global variable is declared in the following way
 
"Variable name" in window

Second, all variable declarations are at the top of the scope scope. Take a look at a similar example:
 


alert("a" in window);
var a;

At this point, even though the declaration is after alert, the alert pops up true, because the JavaScript engine first sweeps through all the variable declarations, then moves them to the top, and the resulting code looks like this:


var a;
alert("a" in window);

Third, you need to understand that the title means that the variable declaration is advanced, but the variable assignment is not, because this line of code includes the variable declaration and the variable assignment.

You can split the statement into the following code:

 
var a;    //The statement < br / > a = 1;    //Initialize the assignment

So the bottom line is that when variable declarations and assignments are used together, the JavaScript engine automatically splits them into two parts in order to advance the declaration of variables, and not advance the assignment because it might affect the code to perform unexpected results.

The code in the title is equivalent to:


var a;
if (!("a" in window)) {
    a = 1;
}
alert(a);

According to the analysis of the above examples, var must be added in front of declared local variables when declaring variables, and var should not be added if global variables are declared (it is better to limit the number of global variables and use local variables as much as possible).

Here are a few features of using var

Using the var statement to declare a variable multiple times is not only legal, it also causes no errors.
If a declaration that is reused has an initial value, it is merely an assignment statement.
If a declaration that is reused does not have an initial value, it will have no effect on the existing variable.
Variables without var declarations exist as global variables. Variables with var declarations are local variables, especially inside functions. And, in tests, the var declaration is faster with var than without it. As much as possible in the function set local variables, which is safe and fast, the variable operation is more reasonable, not because the function random operation of global variables and lead to logic errors.

The best way to declare objects is to use the object itself, which is much faster than the new method.

Variable names are self-chosen, and for the sake of semantics and specification, the variable names may be a little longer, but note that the length of the variable names can also affect the speed of the code. Long variable name declarations don't execute as fast as short ones.


Related articles: