JS (JavaScript) Small talk variable declaration (explicit implicit declaration, declaration in advance)

Source: Internet
Author: User

var a=5;function fun () {a=0;         alert (a);    0 alert (THIS.A);     5 var A;        alert (a); 0}fun ();

The above code will output 0, 5,0.
first of all, our variable declaration assignment in JS is divided into two parts.
1. Variable declaration var a;2. Variable assignment a=2;
But in general we will put the declaration and assignment of variables together is Var a=2;
and the declaration of the variable in JS will be advanced to the beginning of the current scope, that is to say the above code and the following code is equivalent

var a=5;function fun () {    var a;       Declaration of      //variables in advance     a=0;               //assignment statement remains constant     alert (a);          //0    alert (THIS.A);    //5     alert  (a);         //0    } 

so alert (a) is actually the value of the output local variable A, THIS.A is the value of the global variable A, the above is the declaration of the variable is introduced in advance
below our code will be modified as follows

var a=5;function fun () {a=0;         alert (a);    0 alert (THIS.A);        0 alert (a); 0}

at this time all of our output values are 0;
We declare variables to be divided into a display declaration (Var a) and an implicit declaration (a);
when a local variable inside a function is implicitly declared, the variable is not a local variable, but a global variable.
so it is equivalent to assigning a value of 0 to the global variable A, so the output is all a.


This article is from the "11722655" blog, please be sure to keep this source http://11732655.blog.51cto.com/11722655/1789878

JS (JavaScript) Small talk variable declaration (explicit implicit declaration, declaration in 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.