JS (Scope) _ About scope of attention to points

I. Introduction                                                                                     

Recent review when finally the vague concept related to the scope previously made almost the same.

Here are a few on js important role in the domain concept

Second, the main knowledge                                                                              

1, Js because JavaScript uses static scope, scope of a function when the function is defined on the decision.

      For example:

. 1  var value =. 1 ;
 2  
. 3  function foo () {
 . 4      the console.log (value);
 . 5  }
 . 6  
. 7  function bar () {
 . 8      var value 2 = ;
 . 9      foo ();
 10  }
 . 11  
12 is bar (); / /. 1 

/ *
bar () of access are: value = 2, foo (), value. 1 =
foo () that has access to are:. 1 = value



* /

         Note: Due to JS is static scope, after the function definition is good, his scope has been identified. Therefore bar () function call foo () only have access to the value = 1.

     Reference: https://github.com/mqyqingfeng/Blog/issues/3

 

2, the scope of the following three conditions for the global

(1) functions and variables in the outermost layer of the outermost layer has a function of global scope defined

. 1  var the authorName = "hillside streams" ;
 2  
. 3  function doSomething () {
 . 4  
. 5      var blogName = "Dream sky" ;
 . 6  
. 7      function innerSay () {
 . 8  
. 9          Alert (blogName);
 10  
. 11      }
 12 is  
13 is      innerSay () ;
 14  
15  }
 16  
17 Alert (authorName); // hillside creek 
18  
19 Alert (blogName); // script error 
20  
21 doSomething (); // dream of the sky
22  
23 innerSay () // Script Error

 

(2) I = 100; var keyword is not used, direct mode assignment statement is a global variable.

 

. 1  function doSomething () {
 2  
. 3      var the authorName = "hillside streams" ;
 . 4  
. 5      blogName = "Dream sky" ;
 . 6  
. 7      Alert (the authorName);
 . 8  
. 9  }
 10  
. 11 doSomething (); // hillside streams 
12  
13 Alert (blogName); // dream of the sky 
14  
15 Alert (authorName); // script error

 

(3) properties of all window objects have global scope

 

Reference: https://blog.csdn.net/qq_30904985/article/details/81272577

 

3, on the scope chain

(1) active object is a function of: his own arguments (parameters) and defined inside the function variable, then the parent of the active object, look up layer by layer.

 

4, face questions

(1)

var A = 10 ;
         var O = { 
             A: . 11 , 
             B: { 
                 fn: function () { 
                      the console.log (A); 
                 } 
             } 
        } 
        obfn (); // 10 
Description: Because fn: Events () function in only the object of a global

 

(2)

            function foo () { 
            the console.log (value); 
            } 

            function bar () {
             var value 2 = ; 
            foo (); 
            } 
            var value =. 1 ; 
            bar (); // . 1 
Description: foo () to find the own whether active object has a, we found directly export its own active objects a = 1,

 

(3) This situation is a little different from the above, value = 2 only assignment is not defined, it will be promoted to the top

            //var value = 2   
            function foo() {
            console.log(value);
            }
            function bar() {
            value = 2;
            foo();
            }
            var value = 1;
            bar();//2

 

(4) function in vivo function declaration will be lifted

. 1        function A (B) {
 2              the console.log (B);   // function B 
. 3              function B () {
 . 4                  the console.log (B); // function B 
. 5              }
 . 6              B ();
 . 7          }
 . 8          A (. 1 )

The above equivalent code is as follows:

        function A (B) {
              function B () { 
                 the console.log (B); // function B 
             } 
             the console.log (B);   // function B 
            
             B (); 
         } 
         A ( . 1)

 

(5)

  function fun(num){
            console.log(num); //10
            var num = 5;
            console.log(num); //5
        }
        fun(10)

 

(6)

. 1    function Fun (GER) {
 2              the console.log (GER);
 . 3              var GER = function () { // function expression 
. 4                  Alert ( "Hello World" );
 . 5              }
 . 6              the console.log (GER)
 . 7          }
 . 8          Fun (5 )
 9          // analysis wave 
10          // 1. analysis of global variables gO, go to generate the object 
. 11          // the gO = { 
12 is          //      global variable declarations: 
13          //          not skip 
14          //      global function declaration:
15          @      Fun: function; 
16          // } 
. 17          @ 2. line execution 
18          // 3. Analysis of AO, AO generated objects 
. 19          @ AO = { 
20 is          //      1. Analysis Parameters 
21 is          @          GER:. 5 
22 is          //      2. analysis of variable declarations: 
23          //          not skip 
24          //      3. analysis function declaration 
25          //          not skip 
26          //      duplicate names cover 
27          // } 
28          // last output: 5

(7)

1   function Fun (GER) {
 2              the console.log (GER);
 . 3              function GER () {
 . 4                  Alert ( "Hello World" );
 . 5              }
 . 6          }
 . 7          Fun (. 5 )
 . 8          // Analysis wave 
9          // 1. analysis of global variables gO, go generating the object 
10          // the gO = { 
. 11          //      global variable declarations: 
12          //          not skip 
13          //      global function declaration: 
14          @      Fun: function; 
15          // } 
16         @ 2 line by line 
17          // 3. Analysis of AO, AO generate the object 
18 is          // AO = { 
. 19          //      1. analysis parameter 
20 is          @          GER:. 5 
21 is          //      2. Analysis of variable declarations: 
22          //          No skip 
23          //      3. analysis function declaration 
24          @          GER: function;     
25          //      duplicate names cover 
26          // } 
27          // last output: GER function () { 
28              //      Alert ( "Hello World"); 
29              // }

 

Guess you like

Origin www.cnblogs.com/xxm980617/p/11229334.html