Home >Web Front-end >JS Tutorial >Introduction to JavaScript global variable declarations and their advantages and disadvantages

Introduction to JavaScript global variable declarations and their advantages and disadvantages

伊谢尔伦
伊谢尔伦Original
2017-07-18 09:17:402422browse

Declaring global variables in

JavaScript is mainly divided into explicit declaration or implicit declaration:

Declaration method one:

Use var ( Keyword) + variable name (identifier) ​​is declared 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:

<script> 
  var test = 5;//全局变量 
  function a(){ 
    var a = 3;//局部变量 
    alert(a); 
  } 
  function b(){ 
    alert(test); 
  } 
  //a();//调用a方法,那么方法里面的内容才会执行 
  //b();//同上 
</script>

Declaration method two:

Without using var, directly assign a value to the identifier test, which will be implicitly declared. The global variable test. Even if the statement is within a function, test becomes a global variable when the function is executed.

<script> 
    test = 5;//全局变量 
    function a(){ 
      aa = 3;//也是全局变量 
      alert(test); 
    } 
    //a();  //输出5 
    //alert(aa);//这里也可以方法a()方法里面的变量,因为aa是全局变量 
  </script>

Declaration method three:

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

<script> 
  window.test = 50; 
  alert(test);//输出50 
</script>

This This method is often used to expose some functions to the world after an anonymous function is executed. For example, the code of the last sentence
in JQuery1.5 is as follows:

window.jQuery = window.$ = jQuery;

Advantages of global variables:

can reduce the number of variables and reduce the number of variables due to actual parameters and The time consumption caused by data transfer of 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.

JS method to determine whether a variable is empty or null

function isNull(data){
return (data == "" || data == undefined || data == null) ? "暂无" : data;
}

The above is the detailed content of Introduction to JavaScript global variable declarations and their advantages and disadvantages. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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