The difference between "Var" and "Var" is used when declaring "reprint" in JavaScript

Source: Internet
Author: User

Original link: http://www.2cto.com/kf/201204/128406.html "invasion and deletion"

While JavaScript declares variables, while declaring them with the Var keyword and declaring them without the keyword, there are many times when it's not a problem to run, but there's a difference between the two ways.

While JavaScript declares variables, while declaring them with the Var keyword and declaring them without the keyword, there are many times when it is not a problem to run, but there is a difference between the two ways. Code that works correctly does not represent the appropriate code.

var num = 1;

is to declare the variable in the current domain. A local variable (local variable) if declared in the method, or a global variable if declared in the global domain.

and num = 1;

In fact, it is a property assignment operation. First, it tries to chain in the current scope (as declared in the method, then the current scope chain represents the global scope and the method local scope etc ... ), if NUM is found in any current scope chain, the NUM attribute is executed, and if NUM is not found, it will create the NUM attribute and assign a value in the global object (that is, the topmost object of the current scope chain, such as the Window object).

Attention! Instead of declaring a global variable, it creates a property of a global object.

Even so, it may be hard for you to understand the difference between a "variable declaration" and a "Create object property" here. In fact, JavaScript's variable declarations, creation properties, and each of the properties in each JavaScript have a certain flag stating their properties----such as read-only (ReadOnly) non-enumerable (Dontenum) non-deleting (dontdelete), and so on.

Since the variable declaration comes with a non-deleted attribute, the comparison of var num = 1 with num = 1, which is a variable declaration with a non-deleted attribute and cannot be deleted, is a property of the global variable and can therefore be removed from the global variable.

See the following code for details:

Copy CodeThe code is as follows:
NUM1 is a global variable, num2 is a property of window

var num1 = 1;

num2 = 2;

Delete num1; Cannot delete

Delete num2; Delete

function model () {

var num1 = 1; Local variables

num2 = 2; Properties of Window

anonymous functions

(function () {

var num = 1; Local variables

NUM1 = 2; Inheritance scope (closures)

NUM3 = 3; Properties of Window

}())

}

PS. In the ECMASCRIPT5 standard, there is a "strict pattern" (Strict mode). In strict mode, assigning a value to an undeclared identifier throws a reference error, thus preventing accidental creation of global variable properties. Some new versions of browsers are now supported.

Articles you may be interested in:
    • A detailed description of the use of the Var keyword in javascript
    • JavaScript defines variables with Var and non-VAR analysis
    • JavaScript defines variables with the difference between Var and no Var
    • Examples of variables declared in JavaScript with Var and no Var are introduced
    • On the difference of VAR declaration when defining variables in JavaScript
    • A discussion of the difference between Var and no var when defining variables in JavaScript
    • A deep analysis of the difference between the JavaScript global variable var and non-Var
    • Inference about the scope of VAR declaration variables in JavaScript
    • Var_dump function Implementation code in JavaScript
    • "var" in JScript defines the scope of a variable
    • Analysis of the difference between Const, VAR and let in JavaScript

The difference between "Var" and "Var" is used when declaring "reprint" in JavaScript

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.