A question seen

 var number = 5 ;
     var obj = { 
        number: 3 , 
        fn1: ( function () {        // The function will be executed immediately, and 
            var number will be called immediately ;            // Declare a number with the value undefined 
            this .number * = 2;      // Since the function is called immediately and is an anonymous function, this points to window at this time, the global number = 10 
            console.log ( this )      // window 
            number = number * 2;   // The number in the anonymous function is undefined 
            number = 3;            / / Anonymous function is assigned a value of 3 
            return  function () {  // Return a function 
                var num = this .number; 
                console.log ( this )     // According to the caller to decide to point to 
                this .number * = 2 ; 
                console.log (num); 
                number * = 3;   // Through the scope Chain search 
                console.log (number); 
            } 
        }) () 
    } 
    var fn1 = obj.fn1; 
    fn1.call ( null ); // 10 9   
    obj.fn1 (); // 3,27 
    console.log (window. number); // 20    

I was a little ignorant at first, but after understanding step by step, I was suddenly cheerful

Guess you like

Origin www.cnblogs.com/yuliy/p/12740885.html