Currying and partials

// 綁定 this 跟參數
let bound = func.bind(context, arg1, arg2, ...);

// partial function application 綁定部分參數,通常不會使用 this,但公式需要,因此會傳 null
function mul(a, b) {
  return a * b;
}

let double = mul.bind(null, 2);

alert( double(3) ); // = mul(2, 3) = 6
alert( double(4) ); // = mul(2, 4) = 8
alert( double(5) ); // = mul(2, 5) = 10

// 不需要每次都傳入一個固定的參數
let triple = mul.bind(null, 3);

alert( triple(3) ); // = mul(3, 3) = 9
alert( triple(4) ); // = mul(3, 4) = 12
alert( triple(5) ); // = mul(3, 5) = 15

Going partial without context

Currying

Currying? What for?

Advanced curry implementation

Last updated

Was this helpful?