I recently encountered something in JavaScript that I have never seen before, and I have no idea what this type of function invocation is called. I want to learn more about how it works.
Say you have a multiply function that accepts an arbitrary number of arguments. The function will return the product of all of those arguments, like so:
function multiply() {
var x = 1;
for (let i = 0; i < arguments.length; i++){
x *= arguments[i];
}
return x;
}
Calling multiply(1,2,3) would then return 6.
However, you could change the last line, return x, to return a function that runs the multiplication again if additional parameters are supplied. Therefore, you could do something like:
multiply(1,2,3)(1,2)(3)
...and you would get 36.
I had no idea this was possible. What is this chaining of function calls together called? Does anyone know where to find additional examples of this?
EDIT!!!
I forgot that the call would really be:
mulitply(1,2,3)(1,2)(3)()
There's a condition to check if no parameters are present, just to return the current value. This will end the chain of calls.