of this Detailed

  // define: 
    //      declaration 
    // execute: 
    //      function name + () 
    
    // function defined position 
    // Location Execution function 

    // location 

    // the this: the execution context, typically present in the this function, represents the current execution context function, if the function is not implemented, then this is not the content, only the function only after the implementation of this bind 

    

    // position executed !!! 

    // 1. default execution: fn (): this point window, strict point mode undefined 
    // function Fn () { 
    //      "use strict" 
    //      the console.log (the this) 
    // } 
    // Fn () 
    
    // 2. performed by the object (context object by performing implicit execute): obj.fn (): the current execution target 
        // function Fn () { 
        //      the console.log (the this) 
        // } 
        // var a = 10;
        // var obj = {
        //     a:20,
        //     b:fn
        // }
        // obj.b();
        // var obj2 = {
        //     a:30,
        //     b:obj.b
        // }
        // obj2.b();
        // var obj3 = {
        //     a:40,
        //     b:obj2
        // }
        // obj3.b.b()
    
        // 隐式丢失
        // setTimeout(obj.b, 2000);
        // function setTimeout(cb,t){
        //     // t
        //     cb()
            
        // }
    
    //3. performed explicitly (bind function performed by the method): Who is specified, that is, whose 
        function Fn () { 
            the console.log ( the this ) 
        } 
        var F = fn.bind (window); 
        F (); 

        // Get back to the implicit missing this, in fact, use explicit implementation of binding force 
        // the setTimeout (obj.b.bind (obj), 3000); 



    // who performed this function where, this is who 
    

    // scene: 
    //      1. default execution: pointing mode window nonstrict 
    //              window || undefined 
    //      2. performed implicitly: performed by any object 
    //              direct execution object 
    //      3. explicitly execute: a function or bind by call or apply to perform 
    //              write whoever who 


    // function the Fn () { 
    //     the console.log (the this) 
    // } 
    // var obj = {} 
    // obj.fn = Fn; 

    // obj.fn (); // obj 

    // var obtn = document.getElementById ( "BTN"); 
    / / obtn.onclick = Fn; 
    // // // when clicking obtn 

    // fn.bind ( "Hello") (); // "Hello" 

    // var F = fn.bind (obj); 
    // F ( ); obj // 

    // Fn (); // window 




    // 4. executed constructor (executed by new) 



   function Fn (A) { 
         the console.log ( the this ) 
         the console.log (A) 
        the console.log (arguments) 
     } 

     Fn ("ADMIN" ) 

    var F = fn.bind ( "Hello", "zhangsan" ); 
    F ( "the root" ) 


    // the bind (): After the execution, returns a new function, a new function is changed and this old function parameters 
    // one of: a parameter: 
    // parameter: this indicates the directivity 
    @ two kinds: a plurality of parameters: 
    // parameter 1: this indicates the directivity 
    // parameter 2: will function with the parameters of the original on new function 
    // ... 
    // the bind new parameters and the old parameters passed along with new functions 

    // change this point 

    var obj = { 
        name: "obj" , 
        Show: function () { 
            the console.log ( the this .name) 
        } 
    }
    obj.show () 

    var obj2 = { 
        name: "obj2" 
    } 
    obj.show.bind (obj2) 

   
    var name = "zhangsan" ; 
    obj.show.bind (window) (); 
    // the window.name = "John Doe " 

        


    // the this can only be the object

 

Guess you like

Origin www.cnblogs.com/huangping199541/p/11450564.html