Scope of variables in the problem js

First, the first case

. 1          var num = 100 ; 
 2          function Demo () {
 . 3              var num = 101 ; 
 . 4              the console.log (num); // 1. num variables at the same level to find a scope is found is executed, the execution result is 101 
. 5          }
 . 6          Demo ();     

 

Second, the second case

. 1          var num = 100 ; 
 2          function Demo () {
 . 3               the console.log (num);      // 1. There are similar scope num, num and lifting during the pre parsing, the rear attachment details 
. 4               var num = 101 ; 
 . 5          }
 . 6          Demo (); 
 . 7          
. 8              // preresolved Information: 
. 9              // function Demo () { 
10              //     . NUM var; // only 2 lift variable lift variable names, variable values will not lift 
11              / /     the console.log (NUM);.. 3 // variable assignment has not been performed at this time so that the output is undefined 
12 is              //     NUM = 101; 
13 is              // } 
14         

 

Third, the third case

. 1          var NUM = 100;         // 1. firstly lifting functions and variables, the details attached 
2          function Demo () {
 . 3              var NUM = 101 ; 
 . 4          }
 . 5          Demo ();                    
 . 6          the console.log (NUM);         
 . 7          
. 8              // preresolution details: 
. 9              // function Demo () {// a variable lift and 2 when the function, the function of priority. 
10              @ var NUM = 101; 
. 11              // } 
12 is              // var NUM; 
13 is              // NUM = 100 ; 
14          
15              //demo ();. // 3 num case the function is executed and local variables, i.e., 101 
16              // the console.log (num);. // At this time the global variable. 4 num covers local variables, the output 100

 

Fourth, the fourth case

. 1          var NUM = 100;              // 1. firstly lifting functions and variables, the details attached 
2          function Demo () {
 . 3              NUM = 101 ; 
 . 4          }
 . 5          the console.log (NUM);
 . 6          Demo ();
 . 7          Console. log (NUM);
 . 8          
. 9              // preresolved details: 
10              // function Demo () {. 2 // a function and a variable lift, a priority function 
. 11              //     NUM = 101; 
12 is              // } 
13 is              // var NUM; 
14               // NUM = 100; 
15              //console.log (num);. // 3 is output at this time the global variable 100 
16              // Demo ();.. 4 // Run the local variables, so num is 101 
. 17              // the console.log (num); // 5. Since the local variable is not redefined, the call changes the values of global variables when 
18                                  // 5 so num output at this time is changed by the value of the local variable through 101 is the global variable * /


Fifth, the fifth case

. 1          function demo () {
 2              num =. 11 ;
 . 3          }
 . 4          demo ();         // 1. At this time equal num. 11 
. 5          the console.log (num);    
 . 6          // when executed demo, num not defined, will now current Finding no definition of scope, not just to their superiors until found or not found in the global reach, then he will automatically NUM var; 
7          // so when the console is executed, it has a good definition and num = 11 ;

 

 ,,, expansion: If the global variable is not defined, it will not be an error

            // first case: the first assignment and then output 
            NUM = 101;          // At this point, although not defined, but will default to js you add a var 
            console.log (NUM); 

            // second case: first output again assignment 
            console.log (num);     // at this point there is no effect of code num = 101 iS this is not defined num 
            num = 101 ;                    
             // because first of all will, and looking before code is executed at the same level scope of num there is no definition of variables to find to their superiors, did not find, so there num is not defined    

 

Guess you like

Origin www.cnblogs.com/wyctf/p/11401288.html