JavaScript declares global variables and local variables

Source: Internet
Author: User

JS in the declaration of global variables are mainly divided into explicit declarations or implicit declarations are described below respectively.

Declaration Method One:

The use of the VAR (keyword) + variable name (identifier) is declared outside the function as a global variable, otherwise the function declares a local variable. The method is explicitly declared in detail as follows:

?
1 2 3 4 5 6 7 8 9 Ten One A <script>   var test = 5;//全局变量   function a(){     var a = 3;//局部变量     alert(a);   }   function b(){     alert(test);   }   //a();//调用a方法,那么方法里面的内容才会执行   //b();//同上 </script>

Declaration Method Two:

No var is used to assign a value directly to the identifier test, which implicitly declares the global variable test. Even though the statement is within a function, when the function is executed, test becomes a global variable.

?
1 2 3 4 5 6 7 8 9 <script>     test = 5;//全局变量     function a(){       aa = 3;//也是全局变量       alert(test);     }     //a();  //输出5     //alert(aa);//这里也可以方法a()方法里面的变量,因为aa是全局变量   </script>

Declaration Method Three:

Use the window global object to declare that the properties of the global object are also global variables, as detailed below:

?
1 2 3 4 <script>   window.test = 50;   alert(test);//输出50 </script>

This approach is often used when an anonymous function is executed to expose some functions to the global. As the last sentence in JQuery1.5
The code is as follows:

?
1 window.jQuery = window.$ = jQuery;

Advantages of global variables:

You can reduce the number of variables and reduce the time consumption caused by data transfer of actual parameters and formal parameters.

Disadvantages of global variables:

(1) The global variable is stored in a static storage area, which allocates memory for the program to run, and the program ends releasing the memory. Compared with the dynamic allocation and dynamic release of local variables, the lifetime is relatively long, so too many global variables will occupy more memory units.

(2) The global variable destroys the encapsulation performance of the function. function like a black box, usually through the function parameters and return values for input and output, the function of the internal implementation is relatively independent. However, if a global variable is used in a function, the statement in the function body can be accessed by bypassing the function parameter and the return value, which destroys the function's independence and causes the function to rely on the global variable. At the same time, the portability of the function is reduced.

(3) Global variables make the code readability of the function less readable. Because multiple functions may use global variables, the values of global variables may change at any time when the function is executed, which is detrimental to the troubleshooting and debugging of the program.
Therefore, if it is not a last resort, it is best not to use global variables.

Words do not say much, directly on the example:

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 a function is a 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

JavaScript declares global variables and local variables

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.