函数柯里化---实现Function.prototype.bind

Function.prototype.myBind = function(that) {
  if (typeof this !== 'function') {
    throw new Error('must be function');
  }
  const _self = this;
  const args = Array.prototype.slice.call(arguments, 1);
  return function() {
    return _self.apply(that, [...args, ...arguments])
  }
}

function add(num1, num2) {
  return num1 * num2;
}
const res = add.myBind(this, 6);
const res1 = res(2);
console.log('res1', res1); // 12

你可能感兴趣的:(JavaScript,函数式编程,原型模式,javascript,前端)