For example let's take the function add. I want to be able to call:
add(1, 2)(3, 4)
and
add(1, 2)
using same implementation. I have tried writing the add function the following way:
var add = (a, b) => {
return (c, d) => {
return a + b + c + d;
}
}
This will work fine for add(1, 2)(3, 4) but it will not work for add(1, 2). I am not sure how can I check if a function is passed arguments before returning it.
As there is no way for a function (when called) to know whether its return value will also be called like a function or not, the best you can do is to let the return value be callable and have a
valueOfmethod, so that it can be used directly as a number in a larger expression that expects a number:The "trick" here is that the
+operator forces its operand to coerce to a number, and this will implicitly call thevalueOfmethod of the function-object thataddevaluates to. That+operator is just one of the many ways to get thatvalueOfmethod called. A few more examples: