A deep understanding of javascript variable declaration and a deep understanding of javascript

Source: Internet
Author: User

A deep understanding of javascript variable declaration and a deep understanding of javascript

Compared with C/C ++, The for loop in ECMAScript cannot create a local context.

Copy codeThe 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.

Copy codeThe 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:
 
Copy codeThe 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:

Copy codeThe 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:

Copy codeThe 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:

Copy codeThe 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.