I am looking to implement a static variable within a function that meets the following criteria:
- It maintains its value across multiple calls to the function
- It is only accessible within the scope of that function
Below is a basic example of how I am meeting these requirements:
function func(state, input) {
switch (state) {
case 'x':
this.temp = calc(input);
return this.temp;
case 'y':
return this.temp;
}
}
While it works, I am curious if there is a more standard or efficient way to achieve this. Any suggestions for improvement would be appreciated.