The problem of global variables and local variables in JS

Source: Internet
Author: User

1, the operation result of the program is: 100 10 100

var a = ten;  function Test () {3 A =n; Console.log (a);  5 Console.log (this. a);  var A; Console.log (a);} 9 test ();             

Parsing: JavaScript performs a full analysis of the declaration portion of the entire script file (including local variables) before execution, thus determining the scope of the variable, so a within the function will point to the declared local variable since the 6th line declares the local variable a, before the function test executes. , so line 4th outputs 100. Line 5th outputs THIS.A, we all know that the this pointer inside the function points to the caller of the function, where the function test is called by the global object, so the this pointer points to the global object (this is window), so THIS.A = WINDOW.A, the beginning of the life of the global variable a=10, so the 5th row output is 10. The 7th line output is 100 because the local variable A is already assigned a value of 100 on line 3rd, so the value of the local variable A is output directly.

2, the operation result of the program is: undefined 10

var a = +;  function Test () { console.log (a);  var a = ten; Console.log (a);} 7 test ();        

Analysis: Look at the 1th example, there may be alumni think the output is 10 10, but the result is not 10 10, why? Looking closely at the first sentence of the 1th example, JavaScript performs a full analysis (including local variables) on the declaration portion of the entire script file before execution, but cannot parse the variable definition in advance , and in this function, before the 3rd line is executed, It can be assumed that the variable a has been declared, but not defined (here is the assignment), so the 3rd line of output is undefined, after the 4th line A = 10, the value of variable A is 10, so the 5th line output is 10.

3, the operation result of the program is: 100 10 10

var a = +;  function Test () { console.log (a);  4 A = ten; Console.log (a);} test (); 8 Console.log (a);         

Parsing: We know that within the function, the general use of Var declared as a local variable, the use of VAR declaration is generally a global variable, in the test function, a=10 declared a global variable, so the 3rd row of a should output the value of the global variable, and before the function execution has declared a global variable and assigned a value of 100 , so here's the first output 100. Line 4th assigns the global variable a a value of 10, so the value of global variable a becomes 10, so the 5th line outputs 10. On the outside of the function test, line 8th outputs the value of global variable A, because the global variable is re-assigned to 10, so the output is 10.

The problem of global variables and local variables in JS

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.