bind 方法实现

【要求】:实现 bind 方法

【实现】:

// 简单方法
Function.prototype.bind = Function.prototpe.bind || function(context) {
  var me = this;

  return function() {
    return me.apply(context, arguments);
  }
}

// 考虑柯里化的情况,更加健壮的 bind()
Function.prototype.bind = function(context) {
  var args = Array.prototype.slice.call(arguments, 1),
      me = this;

  return function() {
    var innerArgs = Array.prototype.slice.call(arguments),
        finalArgs = args.concat(innerArgs);

    return me.apply(context, finalArgs);
  }
}

☂ 参考:Javascript中bind()方法的使用与实现


原文地址:https://www.cnblogs.com/Ruth92/p/5879899.html