Three ways to declare global variables based on JavaScript _ basics

Source: Internet
Author: User

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

Declaration Way One:

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

Copy Code code as follows:

var test = 5; Global variables
function A ()
{
var cc=3; Local variables
alert (test);
}
Function B () {alert (test);}

declaration mode two:

Without using Var, assign a value directly to the identifier test, which implicitly declares the global variable test. Even if the statement is within a function, test becomes a global variable when the function is executed.

Copy Code code as follows:

Test = 5;//global variable
function A ()
{
Aa=3; Global variables
alert (test);
}

declaration mode three:

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

Copy Code code as follows:

Window.test;
Window.test = 5;

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
Copy Code code as follows:

Window.jquery = window.$ = JQuery;

Advantages of global variables:

You can reduce the number of variables and reduce the time consumed by data transfer due to actual and formal parameters.

Disadvantages of the

global variable: the

(1) global variable is saved in a static storage area, the program allocates memory when it starts running, and the program ends releasing the memory. Compared with the dynamic allocation and dynamic release of local variables, the lifetime is longer, so too many global variables occupy more memory units. The

(2) global variable destroys the encapsulation performance of the function. Functions like a black box, generally through the function parameters and return values for input and output, the function of the internal implementation of relatively independent. However, if a global variable is used in a function, the statement in the function body can bypass function parameters and return values for access, which undermines the independence of the function and makes the function dependent on the global variable. At the same time, it also reduces the portability of the function. The

(3) global variable makes the code of a function less readable. Because more than one function can use global variables, the value of global variables may change at any time when the function executes, which is very detrimental to the troubleshooting and debugging of the program.
Therefore, it is best not to use global variables if it is not a last resort.

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.