I want to utilize a static variable in a function, i.e., a variable which adheres to the following requirements:
- It retains its value over different calls to that function
- It is known (accessible) only within the scope of that function
Here is a generic example of how I am achieving these requirements:
function func(state, input) {
switch (state) {
case 'x':
this.temp = calc(input);
return this.temp;
case 'y':
return this.temp;
}
}
It works fine, but I'd like to know if it's a custom way, and if there's a better way (cleaner, simpler, better practice, etc).
thisduring the call tofunc? How are you callingfunc? I strongly suspect what you're doing fails your second criterion.