I was looking at an article about memoize function in JavaScript and stumbled upon the example below
// same memoize function from before
const memoize = (fn) => {
let cache = {};
return (...args) => {
let n = args[0];
if (n in cache) {
console.log('Fetching from cache', n);
return cache[n];
}
else {
console.log('Calculating result', n);
let result = fn(n);
cache[n] = result;
return result;
}
}
}
const factorial = memoize(
(x) => {
if (x === 0) {
return 1;
}
else {
return x * factorial(x - 1);
}
}
);
console.log(factorial(5)); // calculated
console.log(factorial(6)); // calculated for 6 and cached for 5
In the memoize function above, how is the args
variable defined/come to be? Could anyone help me to understand it?
const memoize = (fn) => {
let cache = {};
return (...args) => {
...
From the call, it seems that the args
is just a variable for the arguments/params from the passed function fn
but I am unable to wrap my head on why and how the code block above is working.
Also, is there a name for this code/design pattern?
Edit I have read about arguments and rest parameters. However the part that I don't understand is how come the ...args
inside memoize function become the parameters from the passed function fn
?
Shouldn't arguments
object or rest parameter ...args
only represents the parameters/arguments from the current function memoize
, i.e. the fn
argument? And in that case args[0]
is the function fn
?