[Native] call, apply, bind basic use, we have resolved some of the source code

 

 

    function fn1(_a,_b){
            console.log (this); // this is a normal function of this window 
            this.a=_a;
            this.b=_b;
        }
        // fn1 (5,6);

        // was objs = {
        //     fn:function(_a,_b){
        //         console.log(this);
        //         this.a=_a;
        //         this.b=_b;
        //     }
        // }

        // console.log(a,b);

        var obj = {c 10};
        // Use call, the first parameter instead of this function (if this function is not written does not make sense, understand the usefulness of the call)
        fn1.call(obj,5,6);
        // call if the first parameter is null, the function of this point source window
        // fn1.call(null,5,6);
        // console.log(obj);

  

 

        was objs = {
            fn:function(_a,_b){
                console.log(this);
                this.a=_a;
                this.b=_b;
            }
        }
        
        var obj = {c 10}; 
        objs.fn.call (obj, 5,6); // obj replaces the function fn objs objects, it is added to a and b obj properties
        console.log(objs,obj);

  

 

 was objs = {
            fn:function(_a,_b){
                console.log(this);
                this.a=_a;
                this.b=_b;
            }
        }

        var obj = {c 10};
        // apply the same role and call, but the function is an array of parameters to the
        objs.fn.apply(obj,[5,6])
        console.log(obj)

  

 

 

 

In this way, we pass a value, you can know a different value that is the maximum value among the.       

var arr = [2,3,4,5,6]; var max = Math.max.apply (null, arr); // solved Math.max (3,4,5,6,7,8,9) apply by using was the Math.max = (3,4,5,6,7,8,9) console.log (de) console.log(max)

  

 

The relationship between class and prototypes, if you add a method to the prototype. Below this is the way

 

 

 

 

 

 

 

        was arr = [1, 2, 3, 4, 5, 6];

        Array.prototype.slice1 = function (start, end) {
            if (start <0) start = this.length + start; // start is negative when the length of the array that is the negative inverse of the +
            if (end < 0) end = this.length + end;   //  同上
            (! Start) if start = 0; // does not fill the time
            // When not fill; if end = this.length (end!)
            was arr = [];
            for(var i=Math.round(start);i<Math.round(end);i++){
                arr.push(this[i]);
            };
            return arr;

        }

        console.log (arr.slice (-2)) // native provided
        console.log (arr.slice1 (-2)) // own imitation



  // var arr = Array.prototype.slice.call (divs); // divs is a dummy array, call this the place of the original

  

// bin bind this

    // var obj = {a 1}
    // // because the callback function needs to be performed only when the trigger, you can not use call and apply, these two will be executed immediately
    // var handler = clickHandler.bind (obj); // this binding is conducive to delete
    //           document.addEventListener("click",handler);
    // // function .bind (objects), this function will be binding for the object, but not executed, when the trigger execution, this is the object
    //         function clickHandler(e){
    //             console.log(this);
    //             document.removeEventListener("click",handler)
    //         }

// This is the binding function below, you can delete the wording
            // var clickHandler=(function(){

            // }).bind(obj);

  

 

Guess you like

Origin www.cnblogs.com/yuanjili666/p/11704586.html