It would be very helpful, if someone explains the working of a curry function. I have read many examples, but not able to grasp it properly. Is it anyhow related to closure.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Currying is just technique, that can make use of any language feature (e.g. closures) to achieve the desired result, but it is not defined what language feature has to be used. As of that currying does not require to make use of closures (but in most of the cases closures will be used)
Here a little example of the usage of currying, with and without the usage of closure.
With the use closure:
function addition(x,y) {
if (typeof y === "undefined" ) {
return function (y) {
return x + y;
}
}
return x + y;
}
var additionRemaining = addition(3); // Currying
additionRemaining(5);//add 5 to 3
With the use of new Function
instead of closure (partial evaluation):
function addition(x,y) {
if (typeof y === "undefined" ) {
return new Function('y','return '+x+' + y;');
}
return x + y;
}
var additionRemaining = addition(3); // Currying
additionRemaining(5);//add 5 to 3