1
  1. Я правильно понял, что встроенный bind может выполнить каррирование ?
  2. Написанный bind не может выполнить каррирование ?

function mul(a, b) {
  return a * b;
};

var q = mul.bind(null, 2);
console.log(q(2,3))

function mul(a, b) {
  return a * b;
};

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

var s = bind(mul, 'context', 3);

console.log(s(2))

1 ответ 1

4

В вашем примере bind не выполняет карринг. Он принимает 2 параметра (func, context). Вы нигде не привязываете аргументы.

Если я правильно понял, вы хотите сделать что-то вроде этого:

function bind(func, context, ...args) {
    return function(...args2) {
      // Заметьте, что я использовал call вместо apply
      return func.call(context, ...args, ...args2);
    }
}

const mul = (a, b) => a * b;

console.log(mul(2, 3)); // 6
console.log(mul(3, 4)); // 12
console.log(mul(4, 5)); // 20

const double = bind(mul, null, 2);

console.log(double(2)); // 4
console.log(double(3)); // 6
console.log(double(5)); // 10

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.