Home >Web Front-end >JS Tutorial >The difference between using 'var' and not using 'var' when declaring in Javascript_Basic knowledge

The difference between using 'var' and not using 'var' when declaring in Javascript_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 17:37:041107browse

When declaring variables in Javascript, although there is no problem in running with the var keyword or without the keyword, there is still a difference between the two methods. Just because code works doesn't mean it's good code.

var num = 1;

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

and num = 1;

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

Attention! It does not declare a global variable, but creates a property of the global object.

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

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

See the following code for details:

Copy code The code is as follows:

// num1 is a global variable and num2 is one of window Properties

var num1 = 1;

num2 = 2;

                                                                                                                                                                                                                                                               // delete num1; Can not delete

                                                                                                         // delete num2;

function model(){

var num1 = 1; // Local variable

num2 = 2; // Attributes of window

                                                                                                                                                                                                                                     // Anonymous function

                      (function(){

var num = 1; // Local variable

                                                                                                                                                                                                                                                                                    num1 = 2;

num3 = 3; // Attributes of window

                                                                                                                                                                                                                     }

                    }

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn