The difference between using "var" and not using "var" when declaring in Javascript

When Javascript declares a variable, although it is declared with the var keyword and declared without the keyword, there is no problem in running it in many cases, but there are still differences between the two methods. Code that works doesn't mean it's suitable code.

var num = 1;

A variable is declared in the current scope. If it is declared in a method, it is a local variable; if it is declared in the global scope, it is a global variable.

while num = 1;

In fact, it is a property assignment operation. First, it tries to resolve num in the current scope chain (as declared in a method, the current scope chain represents global scope and method local scope etc...); if num is found in any current scope chain , the assignment to the num property will be performed; if num is not found, it will create and assign the num property in the global object (that is, the top-level object of the current scope chain, such as the window object).

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

Even so, it may be difficult for you to understand the difference between "variable declaration" and "creating object properties" here. In fact, Javascript's variable declaration, creation properties, and each property in each Javascript have certain signs to describe their properties - such as read-only (ReadOnly), non-enumerable (DontEnum), non-deletable (DontDelete), etc. Wait.

Since the variable declaration has its own non-deletable attribute, compare var num = 1 with num = 1. The former is a variable declaration with an undeletable attribute, so it cannot be deleted; the latter is an attribute of a global variable, so it can be deleted from the global variable .

See the following code for details:

code show as below:

// num1 is a global variable, num2 is an attribute of window

                     var num1 = 1;

                     num2 = 2;

                     // delete num1; cannot delete

                     // delete num2; delete

                     function model(){

                            var num1 = 1; // local variable

                            num2 = 2; // properties of window

                            // anonymous function

                            (function(){

                                   var num = 1; // local variable

                                   num1 = 2; // inheritance scope (closure)

                                   num3 = 3; // properties of window

                            }())

                     }

PS. In the ECMAScript5 standard, there is a "Strict Mode" (Strict Mode). In strict mode, assigning a value to an undeclared identifier will throw a reference error, thus preventing accidental creation of global variable properties. At present, some new versions of browsers already support it.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324501219&siteId=291194637