Different JavaScript global variables and local variables

Original Address: https://www.cnblogs.com/phermis/p/7307209.html

 

Today saw the god written an article, their understanding of global and local variables is not enough deep, and therefore wrote this article to be a summary.

Great God Code + Screenshot understand text as follows:

Analysis: the above code, the variable iis the varcommand declared valid globally, so that only a global variable i. In each cycle, the variable ivalues will change, but the cycle is assigned to the array ainside the function console.log(i), which the ipoint is global i. In other words, all array amembers inside i, point is the same i, causing the output of run-time is the last round of ithe value, which is 10.

 

First, the definition

Local variables: variables declared within a function can only be accessed inside the function.

Global variables: variable outside the function definition, variable throughout the code can be invoked.

Disclaimer keyword: var. JavaScript can use variable hermit, do not declare variables directly. JavaScript variables in the hermit always declared as global variables to use.

 

Second, the variable lift (if not understand this knowledge, then it is easy to pit)

Because of the way JS engine, is to get all the variables are declared in one by one to perform, so the variable declaration statements will be promoted to the head of the current code block.

Note: The block / block method ===> Function refers to a pair of braces {}, avoid, a Function block, and for, while, if, dividing the standard block is not scoped.

 

Third, learn more about

To enhance the effect of code so the code is actually executed this :

Copy the code
<Script type = "text / JavaScript"> 
    var. 1 = A; 
    function test1 () { 
        var A; // I [unassigned when a local variable with the same name as the global variables, local variables precedence over the global variables, it will be covering the global variable, in this case only a declaration has not defined] 
        alert (a); // alert is performed so undefined 
        a = 2; 
        alert (a); 
    } 
    test1 (); 
    alert (a); // undefined 2 
</ script>
Copy the code

 

Fourth, examples

scope JavaScript variables according to the method of divided blocks (i.e. Function of a pair of braces {} divided). Here again I emphasize again: Yes Function block, and for, while, if the block is not the scope of criteria for the classification. Its variables are not promoted.

js in scope only function scope and global scope, when it is not in a function in the global scope. [Reference Address: http: //www.cnblogs.com/zhus/p/6513741.html]

Copy the code
<Script type = "text / JavaScript"> 
    function test2 () { 
        Alert ( "before scope for:" + I); // I [not unassigned undeclared! Use undeclared variable or function full throw a fatal error interrupt script execution] 

        for (var i = 0; i <3; i ++) { 
            Alert ( "in scope for:" + i); // value of i is 0 , 2, when out of the loop when i is 3 
        } 
        Alert ( "After scope for:" + i); // than the value of i is 3, note that in this case has for scope, but retained the value of i is . 3 

        the while (to true) { 
            var j = 1; 
            BREAK; 
        } 
        Alert (j); // the value of j is 1, it is noted at this time has been outside while scope, but still retains the value of j is 1 

        IF (to true) { 
            1 k = var; 
        } 
        Alert (k); // value of k is 1, it is noted at this time has been outside if scope, but retained the value of k 1 
    } 
    test2 ();
    // If at this time (outside the function scope) re-export exists only in this function scope test2 where i, j, k variables God horse effect happen?  
    alert (i);! // error Yes, it is error, because the variable i is not declared (not unassigned, distinguish the first line of the output test2 function), causing the script error, the program is over!  
    alert ( "This line will print output you?"); // not executed   
    alert (j); // not executed   
    alert (k); // not executed   
</ script>
Copy the code

 

 

Reference: http: //blog.csdn.net/zyz511919766/article/details/7276089

Guess you like

Origin www.cnblogs.com/suziyu/p/11223394.html