The variable declaration in JavaScript has the difference between Var and no Var example introduction to _javascript Tips

Source: Internet
Author: User
Tags function definition

This paper discusses the difference between the variable declaration in JavaScript and VAR, and the scope of the variable declaration in JS is in function, so we often see that the method of avoiding the global variable pollution is

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

Within a function, variables with Var and no var declarations are not the same. There is a var declaration of local variables, no VAR, declared global variables, so you can use this outward exposure interface Dongdong.
When declaring variables in a global scope, VAR and var all look the same, and we know that the declared global variable, which is the property of window, is the same, and we find the difference by ECMAScrpit5 the attribute query method provided by the property.

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, found that there is still a difference, we use delete delete properties to verify that the configuration of false properties can not be deleted. That is, the global object's properties cannot be deleted by variable Var, and we also find that the global object properties created by the function declaration cannot be deleted.

Delete FFF; Delete FFA cannot be deleted;//removable delete ffb;//removable delete 
FFC;//removable

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

It is legal and harmless to use the VAR statement to repeat the declaration statement. If the declaration is repeated and has an assignment, it is not different from the general assignment statement. If you try to read a variable that has not been declared, JS will complain.
Within the function scope of JavaScript, the declared variables or intrinsic functions are visible in the body of the function. means that a function may already be available before it is defined. There are two ways to define a function, one is a function-definition expression, the other is a function declaration statement.

function definition expression 
var FNS = functions () { 
//... 
} ; 
//Function declaration statement 
function Fns () { 
//... 

A function declaration statement is "advanced" to the top of an external script or external function scope, so a function declared in this manner can be called by code that appears before it is defined. In a function definition expression, the declaration of a variable is advanced, but the assignment to the variable is not advanced, so the function defined in an expression cannot be invoked before the function definition.

(function () { 
testa ();//Print out Testa 
testb ();//error: Prompt undefined is not a function 
console.log (TESTC); Undefined, if you move to the top, you can have 
function testa () { 
console.log ("Testa"); 
} 
var testb = function () { 
console.log ("Tesb"); 
} 
var TESTC = "TESTC"; 
}) ();

Of course, we declare variables and functions that must conform to basic specifications, variables and function declarations to 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.