极客时间返利平台,你可以在上边通过山月的链接购买课程,并添加我的微信 (shanyue94) 领取返现。
山月训练营之面试直通车 服务上线了,从准备简历、八股文准备、项目经历准备、面试、面经、面经解答、主观问题答复、谈薪再到入职的一条龙服务。

# js 中如何实现 bind

更多描述

提供以下测试用例,注意第二条测试用例,因此 bind 可实现 _.partial(func, [partials]) 类似功能

function f(b) {
  console.log(this.a, b);
}

//=> 3, 4
f.fakeBind({ a: 3 })(4);

//=> 3, 10
f.fakeBind({ a: 3 }, 10)(11);

相关问题:

Issue

欢迎在 Gtihub Issue 中回答此问题: Issue 32 (opens new window)

最简单的 bind 一行就可以实现,而在实际面试过程中也不会考察你太多的边界条件

Function.prototype.fakeBind = function (obj, ...args) {
  return (...rest) => this.call(obj, ...args, ...rest);
};

测试一下

function f(arg) {
  console.log(this.a, arg);
}

// output: 3, 4
f.bind({ a: 3 })(4);

// output: 3, 4
f.fakeBind({ a: 3 })(4);

那我再抄一个加强版吧嘻嘻 《JavaScript 权威指南》P191 ES3 实现 bind

if (!Function.prototype.bind) {
  Function.prototype.bind = function(o /*, args */) {
    var self = this, boundArgs = arguments;
    return function () {
      var i, args = [];
      for (i = 1; i < boundArgs.length; i++) {
        args.push(boundArgs[i])
      }
      for (i = 0; i < arguments.length; i++) {
        args.push(arguments[i])
     }
     return self.apply(o, args)
    }
  }
}

Author

回答者: Vi-jay (opens new window)

Function.prototype.fakeBind = function (target, ...args) {
  return (...rest) =>
    this.apply(target, args.concat(rest).slice(0, this.length));
};

bind 优化版本:考虑 bind 后返回的函数作为构造函数被 new

Function.prototype.bind = function (context, ...args) {
  const self = this;
  const fn = function (...newArgs) {
    self.apply(this instanceof fn ? this : context, args.concat(newArgs));
  };

  fn.prototype = Object.create(this.prototype);

  return fn;
};
Function.prototype.myBind = function (ctx) {
  ctx ??= globalThis;
  ctx = Object(ctx);

  const self = this;
  const args = [...arguments].slice(1);

  function bound() {
    self.call(new.target ? this : ctx, ...args);
  }

  bound.prototype = self.prototype;
  return bound;
};
Last Updated: 2/13/2023, 10:50:25 AM