A deep understanding of javascript variable Declarations _ basics

Source: Internet
Author: User

The For loop in the ECMAScript does not create a local context relative to C + +.

Copy Code code as follows:

For (var k in {a:1, b:2}) {
Alert (k);
}

Alert (k); Although the Loop has ended but the variable k is still in the current scope
At any time, a variable can be declared only by using the var keyword.

The assignment statement above:

A = 10;
This simply creates a new attribute for the global object (but it is not a variable). "Not a variable" is not to say that it cannot be changed, but that it does not conform to the concept of the variable in the ECMAScript specification, so it is "not a variable" (it can be a property of the global object entirely because there is a global object in JavaScript, Such an operation is not declaring a variable but adding a property to the global object.

Let's look at a simple example to illustrate the problem

Copy Code code as follows:

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

First of all, all global variables are the properties of window, the statement var a = 1, equivalent to WINDOW.A = 1;

You can use the following method to detect whether a global variable declares

"Variable name" in window

Second, all the variable declarations are at the top of the range scope, and look at similar examples:

Copy Code code as follows:

Alert ("A" in window);
var A;

At this point, even though the declaration is after alert, alert pops up 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:

Copy Code code as follows:

var A;
Alert ("A" in window);

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

You can split the statement into the following code:

Copy Code code as follows:

var A; Statement
A = 1; Initialize Assignment

So the point is that when the variable declaration and assignment are used together, the JavaScript engine automatically divides it into two parts to advance the variable declaration, and does not advance the value of the assignment because he is likely to influence the unexpected results of the code execution.

The code in the title is equivalent to:

Copy Code code as follows:

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

According to the analysis of the above examples, if you declare a variable in front of a declared local variable must add Var, if the declaration is global variable can not add var (preferably limit the number of global variables, as far as possible using local variables)

Here's a few features that use Var

Declaring a variable multiple times with the Var statement is not only legitimate, but it does not cause any errors.
If a declaration that is reused has an initial value, it assumes the role of an assignment statement.
If a declaration that is reused does not have an initial value, it does not have any effect on the original variable.
A variable that has no VAR declaration is present as a global variable, a variable with the Var declaration, and belongs to a local variable, especially within the function. And, after testing, a VAR declaration is faster than no var. function inside as many as possible to set local variables, so that is safe and fast, variable operation is more reasonable, not because the function of the random operation of global variables and lead to logic errors.

When declaring objects, it is best to use the object's own amount of time, which is much faster than the new method.

Variable names are taken by themselves, and for the sake of semantics and specification, variable names may be slightly longer, but note that the length of the variable name can also affect the execution speed of the code. Long variable name declaration execution speed is not short fast.

Related Article

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.