1

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
2
  • arguments refers to the arguments object of the current function. So save it to another variable and use it instead. Commented Dec 29, 2016 at 0:21
  • "how inner function can perform the task, but the outer function cannot" How should this even be possible? When you call the inner function, the outer function already terminated. '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. Commented Dec 29, 2016 at 0:24

1 Answer 1

2

The inner function is a different function. When you call it (because it has been returned and assigned to cachedFunction), you pass it different arguments.

cachedFunction('foo', 'bar')

complex function should be executed

It isn't.

You never execute complexFunction.

You pass it as an argument to cache, and cache passes it (written in the arguments object) to console.log, but it never gets called.

If you want to call it, then you need to actually do that.

function cache(func) {
  console.log(arguments); //logs { '0': [Function: complexFunction] }

  return function () {
  console.log(func.apply(null, arguments));
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! And sorry, I forgot to remove the comment about execution. I am really interested in where the inner function gets its arguments from. I especially like what you wrote here: "The inner function is a different function. When you call it (because it has been returned and assigned to cachedFunction), you pass it different arguments." Could you please elaborate a bit more?
@dsvorc41: You are calling the inner function here: cachedFunction('foo', 'bar'). Hence it gets the arguments 'foo' and 'bar'.
@dsvorc41 — I quoted the bit of code which passes arguments to the inner function immediately after I said "you pass it different arguments"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.