Skip to content
快看这页儿写了啥...

JS 实现 bind 方法

题干

  • bind

题解

js
Function.prototype.myBind = function (context) {
  if (typeof this !== 'function') {
    throw new TypeError('Error')
  }
  let _this = this
  let arg = [...arguments].slice(1)
  return function F() {
    // 处理函数使用new的情况
    if (this instanceof F) {
      return new _this(...arg, ...arguments)
    } else {
      return _this.apply(context, arg.concat(...arguments))
    }
  }
}

相关

如何改变函数内部的 this 指针的指向

call、apply 及 bind 函数区别

如果一个构造函数 bind 一个对象,用此构造函数创建出的实例会继承这个对象的属性吗?为什么?

贡献者

isboyjc's avatar isboyjc

浏览量(PV)  次  ·  独立访客(UV)  人次
不正经的前端 | 八股 · 欢迎 star ⭐