Home > Web Front-end > JS Tutorial > body text

Detailed explanation of three ways to declare global variables based on JavaScript_Basic knowledge

WBOY
Release: 2016-05-16 17:34:15
Original
1492 people have browsed it

Declaring global variables in JS is mainly divided into explicit declaration or implicit declaration, which are introduced below.

Declaration method one:

Use var (keyword) variable name (identifier) ​​to declare it outside the function, which is a global variable, otherwise it is a local variable declared in the function. This method is an explicit declaration. The details are as follows:

Copy the code The code is as follows:

var test = 5; //Global variable
function a()
{
var cc=3; //Local variable
alert(test);
}
function b(){ alert(test);}

Declaration method two:

Instead of using var, directly assign a value to the identifier test, which will implicitly declare the global variable test. Even if the statement is within a function, test becomes a global variable when the function is executed.

Copy code The code is as follows:

test = 5;//global variable
function a()
{
aa=3; //Global variable
alert(test);
}

Declaration method three:

Use the window global object to declare. The properties of the global object are also global variables. The details are as follows:

Copy the code The code is as follows :

window.test;
window.test = 5;

This method is often used to expose some functions to the world after an anonymous function is executed. . For example, the last sentence in JQuery1.5
Copy the code The 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 transfer of actual parameters and formal parameters.

Disadvantages of global variables:

(1) Global variables are stored in static storage areas. Memory is allocated for them when the program starts running, and the memory is released when the program ends. 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) Global variables destroy the encapsulation performance of functions. A function is like a black box, which generally performs input and output through function parameters and return values. The internal implementation of the function is relatively independent. However, if global variables are used in the function, then the statements in the function body can bypass the function parameters and return values ​​for access. This situation destroys the independence of the function and makes the function dependent on global variables. At the same time, it also reduces the portability of this function.

(3) Global variables make the code of the function less readable. Since multiple functions may use global variables, the value of the global variable may change at any time when the function is executed, which is very detrimental to program error checking and debugging.
Therefore, it is best not to use global variables unless it is absolutely necessary.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template