开发者社区> 问答> 正文

实现一个 Function.bind

实现一个 Function.bind

展开
收起
kun坤 2019-11-28 14:06:23 360 0
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
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载