title: Using JSON.stringify on custom class
body: <p>I'm trying to store an object in redis, which is an instance of a class, and thus has functions, here's an example:</p>
  
  <pre><code>function myClass(){
      this._attr = "foo";
      this.getAttr = function(){
          return this._attr;
      }
  }
  </code></pre>
  
  <p>Is there a way to store this object in redis, along with the functions? I tried <code>JSON.stringify()</code> but only the properties are preserved. How can I store the function definitions and be able to perform something like the following:</p>
  
  <pre><code>var myObj = new myClass();
  var stringObj = JSON.stringify(myObj);
  // store in redis and retreive as stringObj again
  var parsedObj = JSON.parse(stringObj);
  
  console.log(myObj.getAttr()); //prints foo
  console.log(parsedObj.getAttr()); // prints "Object has no method 'getAttr'"
  </code></pre>
  
  <p>How can I get <code>foo</code> when calling <code>parsedObj.getAttr()</code>?</p>
  
  <p>Thank you in advance!</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Got a suggestion to modify the MyClass.prototype and store the values, but what about something like this (functions other than setter/getter):</p>
  
  <pre><code>function myClass(){
      this._attr = "foo";
      this._accessCounts = 0;
      this.getAttr = function(){
          this._accessCounts++;
          return this._attr;
      }
      this.getCount = function(){
          return this._accessCounts;
      }
  }
  </code></pre>
  
  <p>I'm trying to illustrate a function that calculates something like a count or an average whenever it is called, apart from doing other stuff. </p>