开发者社区 问答 正文

实现一个 Function.bind

实现一个 Function.bind

展开
收起
kun坤 2019-11-28 14:06:23 400 分享 版权
1 条回答
写回答
取消 提交回答
  • Function.prototype.bind2 = function(context) {
      if (typeof this !== "function") {
        throw new Error(
          "Function.prototype.bind - what is trying to be bound is not callable"
        );
      }
      var self = this;
      var args = Array.prototype.slice.call(arguments, 1);
      var fNOP = function() {};
      var fbound = function() {
        self.apply(
          this instanceof self ? this : context,
          args.concat(Array.prototype.slice.call(arguments))
        );
      };
      fNOP.prototype = this.prototype;
      fbound.prototype = new fNOP();
      return fbound;
    };
    
    
    2019-11-28 14:06:39
    赞同 展开评论
问答地址: