Implemented call or bind bind ()

A, bind method

Let's look at the interpretation of the bind method on MDN

    bind()Method creates a new function, in bind()is called, this new function thisis specified in bind first argument, the remaining parameters will be used as an argument for calling the new function.

 In other words, bind () method will be:

  • Create a new function (this is it and call, apply a different point)
  • Bind parameter function receives the second and subsequent creation as its parameter

 That bind to create this new function What other characteristics do?

    When you call a function of binding thisto pass parameters to the value of the objective function. If newthe operator binding constructor function, the value is ignored.

 For the following example: when a new function is created by the bind bindFoo as a constructor that creates instances newBindFoo pointing obj bindFoo not binding, but point bindFoo.

var obj={
    name:"Melody"
}
var name="huyang"
function foo(tel){
    console.log(this.name)
    console.log(tel)
}
var bindFoo=foo.bind(obj,"110")

bindFoo()
//Melody
//110

var newbindFoo=new bindFoo();
//undefinde
//110

Second, you can try to achieve now call bind with friends

To achieve the first two characteristics, this binding call using analog bind, and arguments for dividing transmission parameters to achieve the remaining process

= Function Function.prototype.bind2 (context) { 
    	var = Self the this; 
    	var args = Array.prototype.slice.call (arguments,. 1) when the analog // parameter passing the bind 
    	return function () { 
    		var = bindArgs Array.prototype .slice (arguments) // pass the bind simulation parameters during return function 
        	self.apply (context, args.concat (bindArgs) ); // this function returns the modified point to context, and when the bind and bind the after returning binding function argument passed to concat function returns. 
    	} 
}

Function returns the scope chain modified to point to the bind function, this function returns the instance generation can inherit function prototype bind it.

= Function Function.prototype.bind2 (context) { 
    	var = Self the this; 
    	var args = Array.prototype.slice.call (arguments,. 1) when the analog // parameter passing the bind 
    	var foo = function () { 
    		var = bindArgs the Array .prototype.slice (arguments) // simulate the execution of bind parameter passing when the function returns 
        	self.apply (Self instanceof the this the this: context, args.concat (bindArgs)?); 
                // returns due to changes in the bottom of the function prototype is tied given function prototype, when the return function is used as a constructor, this instanceof self instance must be true (instanceof underlying principle is actually determined according to the judgment prototype chain) 
                // when used as an ordinary function, this point window, self point binding function, then the result is false, the time when the result is false, the this point binding context 
    
    	} 
        Foo.prototype = this.prototype 
        return foo 
}

Need some attention:

    arguments have only class attribute length array of objects and can be read by the index, and an array without a slice method and the like, to use an array of arguments methods have to be converted to a real array arguments. Therefore, using Array.prototype.slice.call (arguments), the method calls for slice Array prototype chain call (or Apply), passing as arguments its context, and returns an array of arguments after the conversion.

  

 

Guess you like

Origin www.cnblogs.com/MelodysBlog/p/11601087.html