How to define global variables in JS

Source: Internet
Author: User

Three different methods

1. Define a variable outside of JS's function

var name= ' test ';

function XX () {

alert (name);

}

2. Do not use Var, directly to define the variable, implicitly declares the global variable

Name= ' testing ';

function XX () {

alert (name);

}

This method, even if the variable is within a function, when the function is executed it becomes a global variable----but the function does not execute it is not known by other function, so it is better to define the function outside

3. Use window. The variable name is defined as a global variable, but note: it is recommended to write the window when calling. Variable name, of course, can not write; The document object of our common document.getxxx is window.

Window.name= ' testing ';

function XX () {

alert (window.name);

}

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.

How to define global 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.