The Difference and Realization of Bid Application Call

Keywords: Javascript REST ECMAScript

Links to the original text: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

See also https://blog.csdn.net/qq_40479190/article/details/78324270

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

http://www.ecma-international.org/ecma-262/6.0/ECMA-262.pdf

apply call bind is a function that changes the direction of this when a function is called. apply call will execute the function immediately. apply bind is a function that returns the direction of binding this. the difference between apply and call is that the format of input parameters is different. apply receive array, call receive parameter column, implement the method of apply and call

Function.prototype.call1 = function(obj,...rest){
     var self = this; //this is the function object that calls the call method
     var context = Object(obj) || window
     context.fn = this
     return context.fn(...rest)
     //delete context.fn 
}

Function.prototype.call2 = function(obj){
     var self = this; //this is the function object that calls the call method
     var context = Object(obj) || window
     context.fn = this
     var args = [];
     for(var i=1;i<arguments.length;i++){
        args.push('arguments['+i+']')
     }
     var result = eval('context.fn(' + args +')')
     delete context.fn 
     return result
}

function bar(a,n){
   console.log(a,"a")
   console.log(n,'n')
   return this.value

}

var obj = {value:90}

bar.call1(obj,2,'dfskj')

The bind creates a new function whose this is specified by the first parameter of the bind and the remaining parameters are used when the new function is called.

The first parameter of bind is:

1. When the bind function is called, it is passed to the objective function as this parameter. If the new operator is used for the new function, the value is ignored.

2. When using bind to create a function in setTimeout, the first parameter of bind, if it is the original value, will be converted to Object; (Under the conditions, the original will only be converted to Object)

3. If the parameter list of the bind function is empty, this of the execution scope will be regarded as this of the new function.

Binding functions can also be used new Operator construction, it will behave as if the objective function has been constructed. The value of this provided will be ignored, but the pre-parameters will still provide the simulation function.

Returns a copy of the original function with the specified this and parameters

Application scenarios: Create partial functions (functions with specified parameters); specify functions that determine this

Implementation of bind method from MDN polyfill by call application

if (!Function.prototype.bind) {
  Function.prototype.bind = function(oThis) {
    if (typeof this !== 'function') {
      // closest thing possible to the ECMAScript 5
      // internal IsCallable function
      throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
    }

    var aArgs   = Array.prototype.slice.call(arguments, 1),
        fToBind = this,
        fNOP    = function() {},
        fBound  = function() {
          // When this instance of fBound ==== true, it means that the returned fBound is called as a new constructor
          return fToBind.apply(this instanceof fBound
                 ? this
                 : oThis,
                 // Get the fBound parameter. The function input parameter returned by bind is often passed this way.
                 aArgs.concat(Array.prototype.slice.call(arguments)));
        };

    // Maintaining prototype relationships
    if (this.prototype) {
      // Function.prototype doesn't have a prototype property
      fNOP.prototype = this.prototype; 
    }
    // The downlink code makes fBound.prototype an instance of fNOP, so
    // If the returned fBound is used as the constructor of new and the new object generated by new is passed into fBound as this, the _proto_ of the new object is an instance of fNOP.
    fBound.prototype = new fNOP();

    return fBound;
  };
}

 

Posted by jerdo on Mon, 05 Aug 2019 23:49:38 -0700