The curried function returns a function.
The function being returned is using the x parameter from its outer scope. The only way it can do that is to remember the value that was passed to x for when it (the function that was returned) is called. It does so using a closure.
function curried(x) {
return function(y) {
return x + y;
}
}
const funcForY = curried(1);
console.log(funcForY);
/*
ƒ (y) {
return x + y;
}
*/
Conceptually, you might think of the returned function as having replaced the x parameter with its value, i.e. the argument that was passed to curried (1 in this case).
// funcForY
function (y) {
return 1 + y;
}
So when you call funcForY with its own argument it returns the sum of the initial argument passed to the curried function and its own argument.
funcForY(2) // 3
That is 1 + 2
1 is the initial argument to curried and 2 is the argument passed into funcForY.
bind returns a new function (bound function) with the this keyword set to the provided value. It also takes a list of pre-specified initial arguments (partially applied function).
function impartial(x, y, z) {
console.log(x);
console.log(y);
console.log(z);
return x + y + z;
}
// initial arguments, 1 and 2
const partialFn = impartial.bind(this, 1, 2);
console.log(partialFn);
/*
ƒ impartial(x, y, z) {
return x + y + z;
}
*/
console.log(partialFn(10));
// 1
// 2
// 10
// 13
That is 1 + 2 + 10
1 and 2 are the initial arguments when creating the bound function and 10 is the argument passed into partialFn.