JS 之 bind 的模拟实现

发布于 2023-10-01 22:34:00 字数 1384 浏览 46 评论 0

与 call,apply 不同,bind 调用并没有立即执行函数,而是返回一个新的函数,而且在调用 bind 的时候可以传参,调用 bind 返回的函数时也可以传参(真让人头大,更复杂的是 bind 返回的函数还要考虑当做构造函数使用的场景,也就是 new 调用。所以实现 bind 比 call 和 apply 稍微复杂些。

实现思路

bind 的基本语法如下:

func.bind(thisArg[, arg1[, arg2[, ...]]])

实现的流程如下:

  • 校验调用方是不是函数
  • 校验传入 this 参数是否为空
  • 构造一个函数
  • 判断是否为 new 调用(通过 instanceof)来决定绑定 this 的值
  • 在构造的函数里执行调用,将初始参数和二次传入参数合并成数组,通过 apply 执行
  • 返回函数

代码

Function.prototype.myBind = function(thisArg) {
  if (typeof this !== 'function') {
    throw new TypeError('it must be invoke by function')
  }

  if (thisArg == undefined) {
    thisArg = window
  } else {
    thisArg = Object(thisArg)
  }
  
  const thisFn = this
  const args = Array.prototype.slice.call(arguments, 1)
  
  const fBound = function() {
    const bindArgs = Array.prototype.slice.call(arguments)
    const isNew = this instanceof fBound
    return thisFn.apply(isNew ? this : thisArg, args.concat(bindArgs))
  }

  // 绑定返回函数的原型
  const fNOP = function() {}
  fNOP.prototype = this.prototype
  fBound.prototype = new fNOP()
  
  return fBound
}

测试一下:

const obj = {
  name: '张三'
}
function foo(age, sex) {
  console.log(this.name)
  console.log(age)
  console.log(sex)
}
foo.myBind(obj, 21)('男')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

三岁铭

暂无简介

文章
评论
27 人气
更多

推荐作者

櫻之舞

文章 0 评论 0

弥枳

文章 0 评论 0

m2429

文章 0 评论 0

野却迷人

文章 0 评论 0

我怀念的。

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文