How to maintain JavaScript function variable states (values) between calls?
<p>I am looking for getters and setters functionality but cannot rely on <code>__defineGetter__</code> and <code>__defineSetter__</code> yet.  So how does one maintain a function variable's value between function calls?</p>

<p>I tried the obvious, but myvar is always undefined at the start of the function:</p>

<pre><code>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.
};
</code></pre>

<p>I could always put another <code>FNS._itemCache = []</code> just above the function, but is there a way to encapsulate the values in the function between calls?</p>
