title: Save JavaScript prototype based objects in sessionStorage?
body: <pre><code>var obj = { 
  conn : null,
  first : function(thisIdentity) {
      "use strict";
      var myObj = this;
      $(document).on('click', thisIdentity, function(e) {
      e.preventDefault();
      $.ajax ({ 
          url : some value,
          // other parameters
          success : function(data) {
              myObj.conn = new Connection(data.user_id, "127.0.0.1:80");
              sessionStorage.setItem('connection', JSON.stringify(myObj.conn));
             }
          });
     },
  second : function(thisIdentity) {
      "use strict";
      var myObj = this;
      var conntn = sessionStorage.getItem('connection');
          $(document).on('click', thisIdentity, function(e) {
          e.preventDefault();
          $.ajax ({ 
              url : some value,
              // other parameters
              success : function(data) { 
              var parsedConnection = JSON.parse(conntn);
              parsedConnection.sendMsg(data.id, data.nid);
          }
        });
      }
  };
  var Connection = (function() {
      function Connection(uid, url) {
         this.uid = uid;
         this.open = false;
         this.socket = new WebSocket("ws://"+url);
         this.setupConnectionEvents();
      },
  Connection.prototype = {
      sendMsg : function(id, nid) {
          alert("Working");
      },
      // other functions
      }
  })();
  </code></pre>
  
  <p>So connection is made in the AJAX callback function of first and I store the object in the <code>sessionStorage</code> via <code>JSON</code> but when I use it in the AJAX callback of second then error is coming that</p>
  
  <blockquote>
    <p>TypeError: parsedConnection.sendMsg is not a function</p>
  </blockquote>
  
  <p>Now I understand that may be it is because JSON can be used to store <strong>plain objects</strong> not <strong>prototype-based objects</strong>.</p>
  
  <h3>My question is : <strong>Can any one tell me how to store prototype-based objects via JSON or any other way to implement this?</strong></h3>
  
  <p>I don't want to use <code>eval</code>. Any code, reference would be much appreciated. Thanks!</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I did as @Dan Prince mentioned but then a new problem occurred that now when in <code>sendMsg</code> function I use </p>
  
  <pre><code>this.socket.send(JSON.stringify({
     action: 'message',
     rec: receiver,
     msg: message
  }));
  </code></pre>
  
  <p>Then it stays</p>
  
  <blockquote>
    <p>InvalidStateError: An attempt was made to use an object that is not,
    or is no longer, usable</p>
  </blockquote>
  
  <p>Any inputs? Thanks!</p>