I am looking for getters and setters functionality but cannot rely on __defineGetter__ and __defineSetter__ yet. So how does one maintain a function variable’s value between function calls?
I tried the obvious, but myvar is always undefined at the start of the function:
FNS.itemCache = function(val) {
var myvar;
if( !$.isArray(myvar)
myvar = [];
if( val === undefined)
return myvar;
.. // Other stuff that copies the array elements from one to another without
// recreating the array itself.
};
I could always put another FNS._itemCache = [] just above the function, but is there a way to encapsulate the values in the function between calls?
An alternative way to set a private variable is by wrapping the function definition in an anonymous function:
This way,
myvaris defined in the scope ofFNS.itemCache. Because of the anonymous function wrapper, the variable cannot be modified from elsewhere.