I am trying to capture the arguments that are being passed to callback 'func' function. However, when I try to console log arguments inside my 'cache' function it doesn't give anything except the name of the callback function.
But when I add a secondary inner function, the logging works just fine and it lets me access the arguments received by the callback. I really want to understand how inner function can perform the task, but the outer function cannot.
function cache(func) {
console.log(arguments); //logs { '0': [Function: complexFunction] }
return function () {
console.log(arguments); //logs { '0': 'foo', '1': 'bar' }
}
}
var complexFunction = function(arg1, arg2) { return arg1 + arg2 };
var cachedFunction = cache(complexFunction);
console.log(cachedFunction('foo', 'bar')); // complex function should be executed
argumentsrefers to the arguments object of the current function. So save it to another variable and use it instead.'foo'and'bar'are passed to the inner function. There is no way for the outer function to access those since it isn't even running at that moment. Maybe I'm misunderstanding the question.