Does the JavaScript declare the difference between VAR

Source: Internet
Author: User

This article discusses the difference between a variable declaration in JavaScript and no Var, and the scope of variable declarations in JavaScript is in function units, so we often see ways to avoid global variable pollution:
(function () {
// ...
})();


within the function, variables with Var and no var are not the same. There is a VAR declaration is a local variable, no VAR, declared global variables, so you can take this outward exposed interface Dongdong.
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 ');//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 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.
Delete FFF;//cannot be deleted
Delete FFA;//Can be deleted
Delete FFB;//Can be deleted
Delete FFC;//Can be deleted
The conclusion is that there is a difference between adding VAR and declaring global variables without 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.
//function-Definition expressions
var fns = function () {
// ...
};
//Function declaration statement
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 ();//Print out Testa
Testb ();//ERROR: Hint undefined is not a function
Console.log (TESTC);//undefined, if you move it to the top.
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 the basic specifications, variables and function declarations to be advanced. want to know moreJavaScript Tutorialsknowledge can be logged in e-mentor network.


This article is from the "Add Language" blog, please make sure to keep this source http://yuguotianqing.blog.51cto.com/9292883/1560858

Does the JavaScript declare the difference between 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.