Home  >  Article  >  Web Front-end  >  In-depth understanding of javascript variable declaration_basic knowledge

In-depth understanding of javascript variable declaration_basic knowledge

WBOY
WBOYOriginal
2016-05-16 16:30:501453browse

Compared to C/C, the for loop in ECMAScript cannot create a local context.

Copy code 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
At any time, a variable can only be declared by using the var keyword.

The assignment statement above:

a = 10;
This simply creates a new property on 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 variable concept in the ECMAScript specification, so it is "not a variable" (the reason why it can become a property of the global object is entirely because there is a global in JavaScript Object, this operation is not to declare a variable but to add an attribute a
to the global object.
Let’s look at a simple example to illustrate the problem

Copy code The code is as follows:

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

First of all, all global variables are attributes of window. The statement var a = 1; is equivalent to window.a = 1;

You can use the following method to check whether a global variable is declared

"Variable name" in window

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

Copy code The code is as follows:

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

At this time, even though the declaration is after the alert, the alert still pops up as true. This is because the JavaScript engine will first scan all variable declarations, and then move these variable declarations to the top. The final code effect is like this:

Copy code The code is as follows:

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

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

You can split the statement into code like this:

Copy code The code is as follows:

var a; //Declaration
a = 1; //Initialization assignment

So to sum up, when variable declaration and assignment are used together, the JavaScript engine will automatically divide it into two parts in order to advance the variable declaration. The reason why the assignment step is not advanced is because it may affect the execution of the code. expected result.

The code in the question is equivalent to:

Copy code The code is as follows:

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

According to the analysis of the above example, when declaring a variable, you must add var before the declared local variable. If you declare a global variable, you do not need to 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 the var statement to declare a variable multiple times is not only legal, but also does not cause any errors.
If a reused statement has an initial value, it acts simply as an assignment statement.
If a declaration is reused without an initial value, it will have no effect on the original variable.
Variables declared without var exist as global variables; variables declared with var are local variables, especially within functions. Moreover, after testing, declaration with var is faster than without var. Set up as many local variables as possible in the function, so that it is safe and fast, and the variable operations are more reasonable. Logic errors will not be caused by random manipulation of global variables in the function.

When declaring an object, it is best to use the object's self-face method, which is much faster than the new method.

The variable name is chosen by yourself. In order to take care of semantics and specifications, the variable name may be slightly longer, but please note that the length of the variable name will also affect the execution speed of the code. Declarations with long variable names do not execute as quickly as short ones.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn