Do you really understand the difference between adding var and not adding var

  • 2020-11-25 07:07:04
  • OfStack

Javascript is a product that follows the ECMAScript standard, and the standard of natural ECMAScript should be followed.

Let's start with the definition and usage of the var keyword

The var statement is used to declare variables.

The creation of the JavaScript variable is also called "declaration "1 variable:


var carName;

After a variable is declared, it is empty (with no value).

Is variable replication, the operation is as follows:


carName = "Volvo";

When declaring variables, you can also assign values to variables:


var carName = "Volvo";

grammar


var varname = value;

The parameter value

参数 描述
varname 必须。指定变量名。

变量名可以包含字母,数字,下划线和美元符号。

  • 变量名必须以字母开头
  • 变量名也可以以$和_开头(但1般不这么用)
  • 变量名是大小写敏感的(y和Y是不同的变量)
  • 保留字(如JavaScript关键字)不能作为变量名使用
value 可选。指定变量的值。

注意: 如果变量声明未指定值,其默认值为 undefined

You've read a lot of articles that say avoid declaring global variables implicitly, that you have to add 'var' to a variable, so what's the difference between adding 'var' and not adding 'var'?

So let's look at one piece of code


var a = 'aa';
alert(a); // The pop-up  'aa'
alert(window.a)// The pop-up 'aa' 

See, when you declare a global variable, you add a property to the 'window' object. The following code has the same effect


a = 'aa';
alert(a); // The pop-up  'aa'
alert(window.a)// The pop-up 'aa' 

So "var a = 'aa' "and "a = 'aa'" are both global variables. What's the difference? Look at the following two pieces of code


var a = 'aa';
delete window.a; // false 
a = 'aa';
delete window.a; // true 

All attributes are added to 'window' object, one can be deleted and one cannot be deleted. But adding 'var' is scoped, and not adding 'var' is always adding attributes dynamically to an 'window' object, as shown in the following code


var test = function(){
 a = 'aa';
}
test();
alert(window.a);// The pop-up 'aa' 

Since the window object is a global object, it can be left out by default, as shown in the following section


var test = function(){
 a = 'aa';
}
test();
alert(a);// The pop-up 'aa' 

Now, if you think about it, why can't implicitly declare global variables be deleted, but explicitly declare global variables be deleted?

The reason is that "delete cannot remove properties that are configurable to false ". Some properties of built-in objects are not configurable, such as properties of global objects created by variable declarations or function declarations, as shown in the following code


delete Object.prototype; // false  Non - deletes, this property is not configurable 
var a = 'aa';
delete window.a;//false  Non - deletes, this property is not configurable 
function test(){};
delete window.test;//false  Non - deletes, this property is not configurable  

The global variable declared by 'var' is actually adding a non-configurable property to the 'window' object, while the global variable declared by 'var' is actually adding a configurable property to the 'window' object.

Note that window can be used instead of window in any of the above situations, such as:


var test = function(){
 a = 'aa';
}
test();
alert(this.a);// The pop-up 'aa' 

For the reasons, check out my previous post 'this,this, again discuss this in javascript, super comprehensive '

Below pull out the var keyword in javascript, give you a separate explanation.

We know that when defining variables, we need to use the Var keyword. When using the Var keyword, we need to pay attention to its usage:
The following columns fully illustrate the different execution results of Var with and without global and local variable definitions.


var var01 = 1;
function funtest() {
 document.write(var01);
 var var01 = 0;
} 

Results: undefined


var var01 = 1;
function funtest() {
 document.write(var01);
 var01 = 0;
} 

The result is: 1


 var01 = 1;
function funtest() {
 document.write(var01);
 var var01 = 0;
}

The result is undefined


a = 'aa';
alert(a); // The pop-up  'aa'
alert(window.a)// The pop-up 'aa' 
0

The result is: 1

See how much you know about var in javascript here, I believe that you will gain something more or less from this article. For more information about javascript var, please continue to follow this site, thank you!


Related articles: