Handwriting bind Source

Handwriting bind Source

The method of changing the function of this three points: call bind apply
the same three-point method:
when the function is called the target, changing the value of this point of
the method is a function of the three methods, hanging on Function.prototype

Different points:
the objective function call after call and apply, can be executed directly
target function after a call to bind, does not happen immediately, but returns a new function, call the new function will be performed objective function

Custom function similar to the bind function, mount it to the Function above, then you can use the function to call the function of the custom
to a function expansion method
Function.prototype.customeBind = function (thisArg, ... List) {
the let Self = this;
the console.log (Self) FUNC //
// new function returns a
return function (arg2 ...) {
// this change point of
self.apply (thisArg, [... list, arg2 ...])
}

    }
    function func(...arg){
        console.log(this);
        console.log(arg);
    }
    func.prototype.miaov = function (){
        console.log(123)
    }
    let newFunc = func.bind({a:1},1,2,3,4);
    //自定义bind函数
    let newFunc2 = func.customeBind({a:1},1,2,3,4)
    newFunc(5,6,7,8)
    newFunc2(5,6,7,8);

Realization of the objective function
returns a new function 1. objective function call

2. Call the new function, which is called function for incoming bind this first argument

3. The new function as a constructor is to find a prototype of the objective function, the new function inherited objective function prototype

4. The new function as a constructor this point is the objective function prototype object

note

The objective function: calling bind function, func.bind func it is called the objective function is
a new function: function bind returned,

the function returns 1 as a constructor, to inherit the new function prototype of the objective function
2. Once the new function as a constructor, this objective function should point to the instance of an object

    //构造函数的问题
     Function.prototype.customeBind = function (thisArg,...list){
        let self = this;
        console.log(self)//func

       //自己实现的bind函数,如果把返回的新函数当成构造函数,此时会遇到问题,就是找不到目标函数原型上的方法,解决:放新函数继承目标函数的原型
       let Bound =  function(...arg2){
            //改变this的指向
            // self.apply(thisArg,[...list,...arg2])
            //如果这个函数作为了构造函数,那么目标函数的this,应该执行的是实例对象
            //如果这个不是作为构造函数,目标函数中的this还指向thisArg
            let thisArgSelf = this instanceof Bound ? this : thisArg;
            self.apply(thisArgSelf,[...list,...arg2])
            
        }
        //原型继承
        // Bound.prototype = self.prototype;
        //Object.create用来创建以某一个对象作为原型的对象的
        Bound.prototype = Object.create(self.prototype)//创建一个新的对象,该新对象是以self.prototype作为原型创建出来的
        Bound.prototype.constructor = self

        //返回一个新函数
        return Bound

    }
    function func(...arg){
        console.log(this);
        console.log(arg);
    }
    func.prototype.miaov = function (){
        console.log(123)
    }
    let newFunc = func.bind({a:1},1,2,3,4);
    //自定义bind函数
    let newFunc2 = func.customeBind({a:1},1,2,3,4)
    //构造函数
    let f1 =new newFunc(5,6,7,8)
    console.log(f1.miaov)
    let f2 =new newFunc2(5,6,7,8);
    console.log(f2.miaov)//undefined,因为f2是newFunc2的一个返回值创建出来的,这个返回值就是customeBind函数里面的匿名韩式创建出来的,而这个匿名函数于customeBind没有关联

Guess you like

Origin www.cnblogs.com/lyly96720/p/12110105.html