Function.prototype.bind()

The bind() method creates a new function, and when bind() is called, this of the new function is specified as the first argument to bind(), and the remaining arguments will be used as arguments to the new function.

grammarfunction.bind(thisArg[, arg1[, arg2[, …]]])

  • ThisArg the value passed to the target function as this when calling the binding function.
    • If the new operator is used to construct the binding function, this value is ignored.
    • When using bind to create a function in setTimeout (provided as a callback), any raw values passed as thisArg are converted to Object.
    • If bind’s argument list is empty, or thisArg is null or undefined, this executing the scope will be treated as thisArg for the new function.
  • arg1, arg2, … Arguments that are preset into the argument list of the binding function when the target function is called.
  • The return value is a copy of the original function, with the specified this value and initial arguments.

An implementation of bind

First, we need to understand the binding of this. There are four principles for this binding:

  • The default binding
  • Implicit binding
  • Explicitly bound
  • The new binding

The priorities of the four binding rules increase from top to bottom. The default binding has the lowest priority and the new binding has the highest priority.

Where explicit binding is, apply(…) And the call (…). Method, when calling a function, bind this, which specifies the value of this in the called function

The bind() method creates a new function. When bind() is called, this of the new function is specified as the first argument to bind(), and the remaining arguments will be used as arguments to the new function.


  Function.prototype.myBind = function (context) {
    if(typeof this! = ='function') {throw new TypeError('error')}let _this = this
    let args = [...arguments].slice[1]
    const F =  function() {  
        if (this instanceof F) {
            The instanceof operator is used to check whether the constructor's prototype property appears on the prototype chain of an instance object.
            return _this.apply(this, args.concat(... arguments)) }else {
            return_this.apply(context, args.concat(... arguments)) } } F.prototype =this.prototype
    return F
  }
// f.protoType = this.prototype; This can be done with an empty function:

  Function.prototype.myBind2 = function (context) {
    if(typeof this! = ='function') {throw new TypeError('error')}let _this = this
    let args = [...arguments].slice[1]
    let F2 = function() {}
    let F =  function() {  
        if (this instanceof F2) {
            The instanceof operator is used to check whether the constructor's prototype property appears on the prototype chain of an instance object.
            return _this.apply(this, args.concat(... arguments)) }else {
            return_this.apply(context, args.concat(... arguments)) } } F2.prototype =this.prototype
    F.prototype = new F2()
    return F
  }

Copy the code