bind 实现

 

(function () {
        var slice = Array.prototype.slice;
        Function.prototype.bind = function () {
          var thatFunc = this,
             thatArg = arguments[0];
          var args = slice.call(arguments, 1);
          if (typeof thatFunc !== "function") {
            // thatFunc 应该为应该函数
            throw new TypeError(
              "Function.prototype.bind - " +
                "被调用的应该为一个函数"
            );
          }
          return function () {
            var funcArgs = args.concat(slice.call(arguments));
            return thatFunc.apply(thatArg, funcArgs);
          };
        };
 })();

 

posted @ 2020-09-04 09:56  林中有风  阅读(181)  评论(0编辑  收藏  举报