JavaScript variables declare those things

Source: Internet
Author: User

Declaration and initialization are not the same

Statement
VAR Joe; The Declaration

Initialize
Joe = ' plumber '; The initialization

Declaring a front-facing

You can declare variables at any point in the function by VAR, which act as if they were declared at the top of the function, which is called hoisting (front/top parsing/Pre-parsing). A logic error can occur when you use a variable and then re-declare it in the function soon. Look at the following example:

var"global";(function() {     alert(myname);     var"local";     alert(myname);})();

In this example, you might think that the first alert pops up "global" and the second pops up "local". This may be understandable, because at the first alert, MyName did not declare that the function would naturally look at the global variable myname, but it does not actually work that way. The first alert pops up "undefined" because myname is treated as a local variable of the function (although it is later declared), all the variable declarations are pre-placed at the top of the function. Therefore, in order to avoid this confusion, it is best to pre-declare all the variables you want to use.

The code snippet above performs the following behavior as follows:

var"global";(function() {    var myname;    alert(myname);    "local";    alert(myname);})();
Forget the side effects of Var

Because of the two characteristics of JavaScript, it is surprisingly easy to create global variables unconsciously. First, you can even use variables without declaring them; second, JavaScript has an implicit global concept, meaning that any variable you don't declare will become a global object property. Look at the following code:

function sum(x, y) {    result = x + y;    result;}sum(1, 3);alert(result);

In this code, the function outside the scope of the result is not declared, the code is still working properly, alert will pop up 4. If Var is not used within a function variable, a global variable is generated, which is the side effect of forgetting Var (Side effects when forgetting Var). Therefore, it is important to use as few global variables as possible in order to be a good neighbor to other scripts.

The most important thing to make a global variable less is to always use VAR to declare the variable.

JavaScript variables declare those things

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.