Three ways to declare global variables based on javascript

Source: Internet
Author: User

This article goes from the Script house: http://www.jb51.net/article/36548.htm

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 <Script> 2   varTest= 5;//Global Variables3   functionA () {4     vara= 3;//Local Variables5 alert (a);6   } 7   functionB () {8 alert (test);9   } Ten   //a ();//Call a method, then the contents of the method will be executed One   //b ();//Ibid. A </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 <Script> 2 Test= 5;//Global Variables3     functionA () {4 AA= 3;//is also a global variable5 alert (test);6     } 7     //A (); Output 58     //alert (AA);//The variable inside the method A () method can also be used here because AA is a global variable9 </Script>

Declaration Method Three:

 1  <  script  >  Span style= "COLOR: #008080" >2   window.test  Span style= "Background-color: #f5f5f5; Color: #000000 ">=  50  ;  3   alert (test ); //  output  4  </  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.

Three ways to declare global variables based on javascript

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.