Examples of differences between var and no var in JavaScript variable declaration _ javascript tips-js tutorial

Source: Internet
Author: User
In a function, the variables with var and without var Declaration are different. If var declares a local variable without var and a declared global variable, You can expose the interface to the outside. This article will discuss the differences between var and no var in the variable declaration in JavaScript, the scope of the variable declaration in Js is a function, so we often see that the method to avoid global variable pollution is

(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.

var fff = 2; window.ffa = 3; ffb = 4; this.ffc = 4; var ffftx = Object.getOwnPropertyDescriptor(window, 'fff'); //configurable:false,enumerable:true,value:2,writable:true var ffatx = Object.getOwnPropertyDescriptor(window, 'ffa'); //configurable:true,enumerable:true,value:2,writable:true var ffbtx = Object.getOwnPropertyDescriptor(window, 'ffb'); //configurable:true,enumerable:true,value:2,writable:true var ffctx = Object.getOwnPropertyDescriptor(window, 'ffc'); //configurable: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.

Delete fff; // delete ffa cannot be deleted; // delete ffb can be deleted; // delete ffc can be deleted; // delete

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.

// 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.

(Function () {testa (); // print testa testb (); // error: undefined is not a function console. log (testc); // undefined. If it is moved 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.