There is a difference between var and no var for variable declaration in JavaScript.

Source: Internet
Author: User

There is a difference between var and no var for variable declaration in JavaScript.

In this article, we will discuss the differences between var and no var for variable declaration in JavaScript. The scope of variable declaration in Js is in the unit of function, so we often see a way to avoid global variable pollution.

1

2

3

(Function (){

//...

})();

In a function, the variables with var and without var Declaration are different. If var declares a local variable without var, the declared global variable can be used to expose the interface.
When declaring variables in the global scope, var and var look the same. We know that the declared global variables are the properties of the window. Are they the same, we use the feature query method provided by ECMAScrpit5 to identify the differences.

1

2

3

4

5

6

7

8

Var fff = 2;

Window. ffa = 3;

Ffb = 4;

This. ffc = 4;

Var ffftx = Object. getOwnPropertyDescriptor (window, 'fff'); // retriable: false, enumerable: true, value: 2, writable: true

Var ffatx = Object. getOwnPropertyDescriptor (window, 'ffa '); // retriable: true, enumerable: true, value: 2, writable: true

Var ffbtx = Object. getOwnPropertyDescriptor (window, 'ffb'); // retriable: true, enumerable: true, value: 2, writable: true

Var ffctx = Object. getOwnPropertyDescriptor (window, 'ffc'); // retriable: true, enumerable: true, value: 2, writable: true

Through the above, we found that there was still a difference. We used the delete attribute to verify that the attribute with the configuration property of false could not be deleted. That is, the attributes of global objects declared through the variable var cannot be deleted. We also find that the Global Object Attributes created with the function declaration cannot be deleted.

1

2

3

4

Delete fff; // cannot be deleted

Delete ffa; // it can be deleted.

Delete ffb; // delete

Delete ffc; // deletable

The conclusion is that the declaration of global variables with and without var is different.

It is legal and harmless to repeatedly declare statements using var statements. If the statement is declared repeatedly and has a value assignment, it is no different from the general value assignment statement. If you try to read a variable that has not been declared, Js will report an error.
In the function scope of JavaScript, declared variables or internal functions are visible in the function body. This means that the function may be available before being defined. There are two methods to define a function. One is a function definition expression and the other is a function declaration statement.

1

2

3

4

5

6

7

8

// Function Definition expression

Var fns = function (){

//...

};

// Function declaration statement

Function fns (){

//...

}

The function declaration statement is "advanced" to the top of the external script or external function scope. Therefore, the function declared in this way can be called by defining the previous code. In Function Definition expressions, the declaration of variables is advanced, but the assignment of values to variables is not advanced. Therefore, functions defined in expressions cannot be called before function definition.

1

2

3

4

5

6

7

8

9

10

11

12

(Function (){

Testa (); // print the testa

Testb (); // error: undefined is not a function

Console. log (testc); // undefined. You can move it to the above.

Function testa (){

Console. log ("testa ");

}

Var testb = function (){

Console. log ("tesb ");

}

Var testc = "testc ";

})();

Of course, we declare variables and functions, which must comply with the basic rules, and declare variables and functions in advance.

Related Article

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.