Use & quot; var & quot; and do not use & quot; var & quot; When declaring in Javascript &

Source: Internet
Author: User

When using Javascript to declare variables, although the var keyword is used to declare the variables and the keyword is not used to declare the variables, it is not a problem to run in many cases, but the two methods are different. Code that can run normally does not mean it is suitable code.

Var num = 1;

Declare a variable in the current domain. If it is declared in the method, it is a local variable. If it is declared in the global domain, it is a global variable.

Num = 1;

In fact, it is an operation to assign values to attributes. First, it will try to be in the current scope chain (for example, if it is declared in the method, the current scope chain represents the global scope and the local scope of the method etc ...) If num is found in any current scope chain, the system will assign a value to the num attribute. If num is not found, it creates the num attribute and assigns a value to the global object (that is, the top-level object of the current scope chain, such as the window object.

Note! It does not declare a global variable, but creates a global object attribute.

Even so, it may be difficult for you to understand the difference between "variable Declaration" and "creating object attributes. In fact, the declaration of Javascript variables, the creation of attributes, and each property in Javascript has a certain flag indicating their attributes-for example, read-only (ReadOnly) Non-enumeration (DontEnum) cannot be deleted (DontDelete) and so on.

Because the variable Declaration contains non-deletable attributes, var num = 1 and num = 1 are compared. The former is a variable declaration, and the attribute cannot be deleted; the latter is an attribute of the global variable, so it can be deleted from the global variable.

For details, see the following code:

Copy codeThe Code is as follows:
// Num1 is a global variable and num2 is an attribute of window.

Var num1 = 1;

Num2 = 2;

// Delete num1; cannot be deleted

// Delete num2; delete

Function model (){

Var num1 = 1; // local variable

Num2 = 2; // window attributes

// Anonymous Function

(Function (){

Var num = 1; // local variable

Num1 = 2; // inherited scope (closure)

Num3 = 3; // window attributes

}())

}

PS. In ECMAScript5, there is a Strict Mode ). In strict mode, if you assign a value to an undeclared identifier, a reference error is thrown. This prevents unexpected creation of global variable attributes. Currently, some new versions of browsers are supported.

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.