I am practising ES6 and I have this code:
const over = (...fns) => (...args) =>
fns.map(fn => fn.apply(null, args));
const minMax = over(Math.min, Math.max);
console.log(minMax(1, 2, 3, 4, 5));
console.log(minMax(1, 2, 5, 4, 3));
console.log(minMax(1, 2, 5, -4, 3));
The goal is to get the minimum and the maximun values between the numbers passed as arguments.
I could understand almost everything, the dynamic is very clear, with one exception, I know that args
refers to the parameters coming from minMax()
, but I couldn't get how the code recognize it.
My guess is: since we have two functions, over()
and minMax()
, when called, they are automatically read in this order, that is why the code knows that the first anonymous function refers to over()
and the second one to minMax()
. But this is just a guess, I don't know if I am right.
What is exactly happening here?