javascript variable scope

1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4      <meta charset="utf-8">
 5      <title></title>
 6 </head>
 7 <body>
 8      <script type="text/ javascript">
 9          /* 
10       The scope of a variable is the area in the program source code where the variable
 11 is defined       .
12       Global variables have global scope, however variables declared inside functions
 13       are defined only in the function body, they are local variables, the scope is local
 14       Function parameters are also local variables, they are only defined in the function body */ 
15  
16       // Local variables have higher priority than global variables of the same name 
17       // ---->
      // ----> global variables are covered by local variables 
19       var scope="global"; // declare a global variable 
20       function checkscope() {
 21            
22            var scope="local"; // declare the same local Variable 
23            return scope; // Return the value of the local variable, not the value of the global variable 
24  
25  
26       }
 27        checkscope()
 28  
29        console.log(checkscope()) // "local" 
30  
31        // ---> 1--The var statement can be omitted when writing code in the global scope, and the var statement must be used when declaring local variables 
32        scope="global"; // Declare a global variable, even without var 
33       function checkscope2(){
 34            scope="local";   // Modified global variable 
35            myscope="local" // A new global variable is explicitly declared here 
36            return [scope,myscope]; // Return two values 
37        }
 38        checkscope2() // Function call 
39        console.log(checkscope2()) // =>["local","local"] 
40        // ---->2--Function definitions can be nested , since each function has its own scope, there will be several cases of local scope nesting 
41  
42        var scope="global scope"; // global variable 
43        function checkscope(){
 44           var scope="local scope"; // local variable 
45            function nested(){
 46                var scope="nested scope"; // local variable in nested scope 
47                return scope; // return value in current scope 
48  
49            }
 50            nested() // nested function call 
51  
52        }
 53        checkscope() // => "nested scope" 
54     
55     /* 
56     function scope and declaration advance
 57      1---> C-like programming -- Each piece of code inside curly braces has its own scope58
 and            variables are not visible outside the piece of code in which they are declared, called block scope
59            There is no block scoping in javascript
 60            JavaScript uses function scoping instead:
 61            Variables have definitions in the body of the function they are declared in and in any function body that is nested within that body
 62  
63     */ 
64     // ---- 1--> 
65     function test(o){
 66         var i=0; // i is defined in the entire function body 
67         if ( typeof o=="object" ){
 68             var j=0; // j is defined in the function body, not only in this code segment 
69             for ( var k=0;k<10;k++){ // k is defined in the function body, not only in the inner 
loop70                 console .log(k); //Output numbers 0~9 
71  
72  
73             }
 74  
75             console.log(k);   // k is defined, output 10 
76         }
 77         console.log(j);   // j is defined, but probably not initialized 
78     }
 79     // javascript's function scope means that all variables declared inside a function are always visible within the function body 
80     // means the variable is even available before it is declared, javascript this feature is informally called declaration ahead of time 
81  
82     // All variables declared in a javascript function are advanced to the inside of the function body 
83     var scope="global" ;
 84     function f(){
 85  
86         console.log(scope); //Output undefined ," instead of "global" 
87         var scope="local"; // The variable is initialized here, but the variable itself is defined anywhere in the function body 
88         console.log(scope); // output "local" 
89     }
 90  
91  
92      </script>
 93  
94 </body>
 95 </html>

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325354760&siteId=291194637