最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How to maintain JavaScript function variable states (values) between calls? - Stack Overflow

programmeradmin4浏览0评论

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?

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?

Share Improve this question asked Oct 2, 2011 at 8:49 Zachary ScottZachary Scott 21.2k35 gold badges124 silver badges208 bronze badges 1
  • 1 Are you missing a closing parenthesis – mowwwalker Commented Oct 2, 2011 at 8:55
Add a ment  | 

3 Answers 3

Reset to default 4

An alternative way to set a private variable is by wrapping the function definition in an anonymous function:

(function(){
    var myvar;
    FNS.itemCache = function(val) {
        if( !$.isArray(myvar))
            myvar = [];
        if( typeof val == "undefined")
            return myvar;
        .. // Other stuff that copies the array elements from one to another without
           // recreating the array itself.
    };
})();

This way, myvar is defined in the scope of FNS.itemCache. Because of the anonymous function wrapper, the variable cannot be modified from elsewhere.

You can store the value on the function by using arguments.callee as a reference to the current function:

FNS.itemCache = function(val) {
    if( !$.isArray(arguments.callee._val)
        arguments.callee._val = [];
    if(val === undefined)
        return arguments.callee._val;
    .. // Other stuff that copies the array elements from one to another without
       // recreating the array itself.
};

However, this will break if the function is stored in a prototype and thus used by more than one object. In this case you have to use a member variable (e.g. this._val).

this is a standard pattern for creating your static variable and for creating private members of an object

FNS.itemCache = (function() {
  var myvar;
  if( !$.isArray(myvar)
      myvar = [];
  return function(val) {
      if( val === undefined)
          return myvar;
         .. // Other stuff that copies the array elements from one to another without
         // recreating the array itself.
  }
})();
发布评论

评论列表(0)

  1. 暂无评论