The variable declaration in JavaScript has the difference between Var and no Var

Source: Internet
Author: User
Tags function definition

This article discusses the variable declaration in JavaScript has the difference between Var and no Var, about the scope of variable declarations in JS is in function units, so we often see the way to avoid global variable pollution is

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

Within the function, variables with Var and no var are not the same. var declares a global variable with a local variable, no Var, and a declaration.

When declaring a variable in the global scope, VAR and no Var all look the same, we know that the declared global variable, which is the property of window, is exactly the same, 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‘ );

var ffatx = Object.getOwnPropertyDescriptor(window, ‘ffa‘ );

var ffbtx = Object.getOwnPropertyDescriptor(window, ‘ffb‘ );

var ffctx = Object.getOwnPropertyDescriptor(window, ‘ffc‘ );The result is: delete fff;  // 无法删除 delete ffa;  // 可删除 delete ffb;  // 可删除 delete ffc;  // 可删除through the above, found that there is still a difference, we then use the Delete Delete property to verify that the configuration of false properties cannot be deleted. That is , the properties of global objects cannot be deleted through Var, and we also find that the global object properties created by the function declaration cannot be deleted. In short, there is a difference between adding Var and not adding Var.   repeating statements using the VAR statement is legal and harmless. If the declaration is repeated and assigned, then it is not different from the general assignment statement. If you try to read a variable that has not been declared, JS will error.
Within a function scope of JavaScript, declared variables or intrinsic functions are visible within the body of the function. means that the function may already be available before it is defined. There are two ways to define a function, one is a function definition expression and one is a function declaration statement. // 函数定义表达式 var fns = function (){ // ... }; // 函数声明语句 function fns(){ // ... } A function declaration statement is "advanced" to the top of an external script or an outer function scope, so a function declared in this manner can be called by the code that it appears before it is defined. In the function definition expression, the declaration of the variable is advanced, but the assignment to the variable is not advanced, so the function defined in the expression cannot be called until the function definition. ( function () { testa();  // 打印出testa testb();  // 报错:提示undefined is not a function console.log(testc);  //undefined,如果移到上面就可以了 function testa() { console.log( "testa" ); } var testb = function () { console.log( "tesb" ); } var testc = "testc";
})();so, when declaring variables and functions, we try to make the declarations of variables and functions in advance.  

The variable declaration in JavaScript has the difference between Var and no Var

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.