Three methods for declaring global variables based on JavaScript

Source: Internet
Author: User

Declared global variables in JS are mainly divided into explicit declaration or implicit declaration.

Method 1:

Using var (keyword) + variable name (identifier) is declared outside the function, that is, global variables. Otherwise, local variables are declared in the function. The explicit statement is as follows:
Copy codeThe Code is as follows:
Var test = 5; // global variable
Function ()
{
Var cc = 3; // local variable
Alert (test );
}
Function B () {alert (test );}

Method 2:

If var is not used, assign a value to the identifier test, which implicitly declares the global variable test. Even if the statement is in a function, after the function is executed, test becomes a global variable.
Copy codeThe Code is as follows:
Test = 5; // global variable
Function ()
{
Aa = 3; // global variable
Alert (test );
}

Method 3:

The window global object is used for declaration. The Global object attributes correspond to global variables as follows:
Copy codeThe Code is as follows:
Window. test;
Window. test = 5;

This method is often used to expose some functions to the global environment after an anonymous function is executed. For example, the last sentence in JQuery1.5
Copy codeThe Code is as follows:
Window. jQuery = window. $ = jQuery;

Advantages of global variables:

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

Disadvantages of global variables:

(1) The global variables are stored in the static storage area. When the program starts running, the memory is allocated. When the program ends, the memory is released. Compared with Dynamic Allocation and release of local variables, the survival time is relatively long, so too many global variables will occupy a large amount of memory units.

(2) global variables damage the function encapsulation performance. A function is like a black box. It is generally input and output through function parameters and return values, and is relatively independent within the function. However, if a global variable is used in a function, the statements in the function body can bypass function parameters and return values for access. This damages the independence of the function and enables the function to depend on global variables. At the same time, it also reduces the portability of the function.

(3) global variables reduce the readability of the function code. Because multiple functions may use global variables, the value of global variables may change at any time during function execution, which is very unfavorable for program troubleshooting and debugging.
Therefore, we recommend that you do not use global 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.