title: Javascript: Creating a function with state
body: <p>I want to build a javascript function that maintains state.  Here's a pattern that I've come up with, but something in the back of my mind tells me this is an anti-pattern.</p>
  
  <pre><code>function f() { 
      var state = 1;
      f = function() {
          return state++;
      };
      return f();
  };
  </code></pre>
  
  <p>Is there anything wrong with this?  If so, what's a better approach?</p>