Preserving local variables when calling function again

I have a function that deletes part of an object, calls another function, and then that other function calls the original after doing some more stuff.

I need the current state of that variable to be saved, so that when I remove a key when the function is called the first time, that key is still gone when called the second time.

As is, the onclick event calls testFunction while passing a mouse event.

How can I instead call the function while preserving the local variable, or at least one local variable?

This is a simplified version of the structure of my code to show what I mean:

var dict_obj = {'test1': 'test_1', 'test2': 'test_2'}

function testFunction(dict_obj) {
  if (dict_obj) {
    delete Object.keys(dict_obj)[0]
      someOtherFunction()
  } else {
    console.log('Function complete')
  }
}

function someOtherFunction() {
  button.onclick = testFunction
}

Is there someway to do this without declaring a global variable or continually passing the parameter through an arrow function with the onclick? In my actual code I have a lot of potential "paths" the function can go through, so passing it locally isn't ideal since the code would get convuluted.



Read more here: https://stackoverflow.com/questions/68461720/preserving-local-variables-when-calling-function-again

Content Attribution

This content was originally published by CrystalGarnet at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: