5

A function is getting called multiple times is there a way to store the context/arguments of last function call and check with current ones.

amitava mozumder
  • 788
  • 6
  • 17

2 Answers2

17

When defining the function, I'd use a closure to store a persistent variable, reassigned to the arguments passed on every call, eg:

const fn = (() => {
  let lastArgs;
  return (...args) => {
    console.log('function was called with args:', args);
    console.log('past args were:', lastArgs);
    lastArgs = args;
  };
})();

fn('foo', 'bar');
fn('baz');
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • 2
    That is awesome – Spangle Jul 24 '19 at 06:47
  • 1
    There was an interesting challenge about this in codewars, as far as I can remember. – briosheje Jul 24 '19 at 06:53
  • are there more ways to accomplish this, I would like your feedback ? – Kunal Mukherjee Jul 24 '19 at 06:53
  • 1
    @KunalMukherjee I'm not sure whether there is a cleaner way than this one. It's clear and flexible enough, not sure whether its worth to look for another solution :P – briosheje Jul 24 '19 at 06:55
  • 2
    @KunalMukherjee You can also assign to an outside variable, but that means that the `lastArgs` loses its closure privacy. Another option is to assign to a property of the function, eg `function foo(...args) { foo.lastArgs = args }`, but functions *shouldn't* have non-standard properties like that. IMO the closure solution is the best by far, the others aren't worth thinking about. Another *odd* solution would be to reassign the function every time you call it, I guess – CertainPerformance Jul 24 '19 at 06:55
  • @CertainPerformance works fine in console, not in my project. the args is undefined so lastarg is always set to undefined – amitava mozumder Jul 24 '19 at 07:06
  • 1
    @amitavamozumder It sounds like you're using code different from the code in the answer - argument rest syntax will *never* be `undefined` (at worst, it'll be an empty array), can you show a [MCVE]? – CertainPerformance Jul 24 '19 at 07:08
  • @CertainPerformance yes, pastargs is redefining every time and getting undefined ...args is empty array. – amitava mozumder Jul 24 '19 at 07:12
  • self invocation issue with my framework. It's working now. – amitava mozumder Jul 24 '19 at 07:18
  • @CertainPerformance losing context of this inside return, saving it as another var after lastArg doesn't hep either. any idea why ? – amitava mozumder Jul 24 '19 at 08:21
  • @amitavamozumder Hard to say without seeing the code. Keep in mind that arrow functions inherit their `this` from the surrounding scope, not from the calling context – CertainPerformance Jul 24 '19 at 08:33
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/196907/discussion-between-amitava-mozumder-and-certainperformance). – amitava mozumder Jul 24 '19 at 08:42
  • One bad thing about this though, is that this function does nothing. Would be cool to see this working with a real function. – Spangle Jul 25 '19 at 05:37
1

You can use a global variable for storing data. Everytime a new function called check new arguments with global variable and do what you want.

burakarslan
  • 245
  • 2
  • 11