JavaScript variable declaration has the difference between var and no var sample introduction

  • 2020-03-30 03:58:37
  • OfStack

This article discusses the difference between variable declaration in JavaScript with var and without var. The scope of variable declaration in Js is a function, so we often see the method to avoid global variable pollution is


(function(){ 
// ... 
})();

Inside a function, variables with var are not the same as variables without var declarations. Local variables are declared with var, and global variables are declared without var, so you can expose the interface to the outside.
When we declare variables in global scope, both var and no var look the same. We know that the declared global variable is the property of the window, and whether it is the same or not, we find the difference through the property query method of the property provided by ECMAScrpit5.


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 find that there is still a difference, and we use delete to delete the property to verify that the property with false configurability cannot be deleted. In other words, the properties of a global object declared by var cannot be deleted, and we will also find that the properties of a global object created by var and function declaration cannot be deleted.


delete fff; //Unable to delete
delete ffa; //Can be deleted
delete ffb; //Can be deleted
delete ffc; //  Can be deleted 

The conclusion is that there is a difference between declaring global variables with var and without var.

Repeating statements using var statements is legal and harmless. If you repeat the declaration with an assignment, it is no different from a normal assignment statement. If you try to read an undeclared variable, Js will report an error.
Within the scope of JavaScript functions, declared variables or internal functions are visible within the body of the function. This means that the function may already be available before it is defined. There are two ways to define a function: a function definition expression and 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 outer script or outer function scope, so the function declared in this way can be called by the code that appears before it is defined. In a function definition expression, variables are declared earlier, but assignments to variables are not, so an expression-defined function cannot be called before the function is defined.


(function() { 
testa(); //Print out the testa
testb(); //Error: undefined is not a function
console.log(testc); //Undefined, if I move it up there
function testa() { 
console.log("testa"); 
} 
var testb = function() { 
console.log("tesb"); 
} 
var testc = "testc"; 
})();

Of course, when we declare variables and functions, we have to follow the basic specification, and we have to declare variables and functions in advance.


Related articles: