The difference between JavaScript in VaR and not var

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

The difference between JavaScript in VaR and not var

Scoped variable statement in Js is to function as a unit, so we often see the method to avoid global variables of pollution is

(function(){

// ...

})();

Inside the function, VaR and no var statement variables is not the same. A VAR statement is a local variable, not VaR, global variable declarations, and so can be exposed to the outside interface.

In the global scope when you declare a variable, VaR and not var all look the same, as we know, the global variable declarations, is the window attribute, whether the same query method, characteristic attributes we provided by ECMAScrpit5, the difference was found between the.

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 findings, the original, there are still differences, we use delete to remove the property to verify the configuration of false, the property cannot be deleted. It is through the property of the VaR variable declared global object cannot be deleted, we also find the global object attribute and function declarations create also 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, declare global variables with VaR and without VaR are different.

Use the VaR statement to repeat the statement is legitimate and harmless. If the repeated statement with the assignment, then and assignment statements generally have no difference. If you try to read not declared variables, Js will be in error.

Function scope JavaScript, variable or function declaration, are visible in the function body. Means, the function may have available before definition. The function definition in two ways, one is the function definition expression, is a function declaration statement.

// The function definition expression

var fns = function (){

// ...

},

// Function statement

function fns(){

// ...

}

Function declarations in “ in ” to the top of the external script or external function scope, function, so in this way the statement, calling it can be re defined before the emergence of the code. The function definition expression, variable declarations are in advance, but to the variable assignment is not ahead of time, so, the function definition to expression mode in a function definition cannot be invoked before.

(function() {

testa(); // Print testa

testb(); // Error: undefined is not a function

console.log(testc); //Print testc

function testa() {

console.log("testa");

}

var testb = function() {

console.log("tesb");

}

var testc = "testc";

})();

Of course, we declare a variable and function, must abide by the basic norms, variable and function declarations to advance.

The above function is defined, in "JavaScript authoritative guide" involved, borrows its statements describe, in addition, also has the similar description.

Ps:'s worship of "JavaScrpt programming" translation of the author Li Songfeng, always will JavaScript explain so deep, but, in the above link blog, you'll find out nouns scope was Li Songfeng Bryant literally use, before he wrote about the prototype of the blog, the scope is not defined.

 This paper first 


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Augustine at November 15, 2013 - 12:38 PM