The difference between using var and not using var in JavaScript

Javascript declares variables. Although there is no problem in running with var keyword declaration and without keyword declaration, there is still a difference between the two methods. Code that can run normally does not mean that it is suitable code.

varnum=1;

Is to declare variables in the current domain. If it is declared in a method, it is a local variable, and if it is declared in a global domain, it is a global variable.

and

num = 1;

In fact, it is an attribute assignment operation. First, it will try to resolve num in the current scope chain (if declared in a method, the current scope chain represents the global scope and the method local scope etc...), if it finds num in any current scope chain , It will perform the assignment to the num attribute. If num is not found, it will create and assign the num attribute in the global object (that is, the topmost object of the current scope chain, such as the window object).

note! It does not declare a global variable, but creates a property of a global object.
  Even so, it may be difficult for you to understand the difference between "variable declaration" and "create object properties" here. In fact, JavaScript variable declarations, creating properties, and each property in each JavaScript have certain flags indicating their properties-such as read-only (ReadOnly), non-enumerable (DontEnum), non-deletable (DontDelete), etc. Wait.

Since variable declarations have non-deletable attributes, compare varnum=1 with num=1. The former is a variable declaration with non-deletable attributes, so it cannot be deleted; the latter is an attribute of global variables, so it can be deleted from global variables.

When Javascript declares variables, although there is no problem in running with var keyword declaration and without keyword declaration, there is still a difference between the two methods. Code that can run normally does not mean that it is suitable code.

varnum=1;

Is to declare variables in the current domain. If it is declared in a method, it is a local variable, and if it is declared in a global domain, it is a global variable.

and

num = 1;

In fact, it is an attribute assignment operation. First, it will try to resolve num in the current scope chain (if declared in a method, the current scope chain represents the global scope and the method local scope etc...), if it finds num in any current scope chain , It will perform the assignment to the num attribute. If num is not found, it will create and assign the num attribute in the global object (that is, the topmost object of the current scope chain, such as the window object).

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

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

Since variable declarations have non-deletable attributes, compare varnum=1 with num=1. The former is a variable declaration with non-deletable attributes, so it cannot be deleted; the latter is an attribute of global variables, so it can be deleted from global variables.

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

varnum1=1;

num2 = 2;

//deletenum1; cannot be deleted

//deletenum2; delete

functionmodel () {

varnum1=1;//local variable

num2=2;//window attributes

//Anonymous function

(function(){

varnum=1;//local variable

num1=2;//Inheritance scope (closure)

num3=3;//window attributes

}())

}

Finally: in the ECMAScript5 standard, there is a "strict mode" (StrictMode). In strict mode, assigning a value to an undeclared identifier will throw a reference error, so it can prevent accidental creation of global variable properties. Currently some new versions of browsers have been supported.

Guess you like

Origin blog.csdn.net/ZYDX18984003806/article/details/103687863