title: Saving Javascript object
body: <p>I have tree of javascript objects. Let's call it "family. It can contain any number of certain objects ("parents") which can each contain any number of "child" objects. The number of levels in this structure is known and each level of the tree only contains objects of one certain type.</p>
  
  <p>All the objects have data and methods.</p>
  
  <p>I want to save the structured data in the databese. JSON.stringify() does it perfect extracting the data and also saving the structure. But how to get back to objects? JSON.parse() fails, because it recreates the object without methods.</p>
  
  <p>What should I do in this case? Should I write my own function for recreating the object from string? Or should I save the data together with methods somehow (seems a waste).</p>
  
  <p>As I know the structure, it would be very handy if there would be a possibility to point to an object and tell "that's a parent object" and it would get the methods. I could easily cycle through it then. But I don't know how to that and I'm also afraid that my constructors could set some values to the default ones.</p>
  
  <p>The objects constructors would look something like this:</p>
  
  <pre><code>function lines()
  {
      this.lines = [];
      this.height = 0.5*theMargin;
  
      this.addLine = addLine;
  
      function addLine(newline)
      {
          this.lines.push(newline);
          this.height += newline.height;
      }
  }
  
  function aLine()
  {
      this.dots = [];
      this.height = 0;
      this.length = indent;
  
      this.insertDot = insertDot;
  
      function insertDot(pos,newDot)
      {
          this.dots.splice(pos,0,newDot);
          this.length += newDot.length;
          this.height = Math.max(this.height,newDot.height);
          if (this.length &gt; maxLineLength)
          { "I will not go into details here" }
      }
  }
  </code></pre>
  
  <p>Then I would do like:</p>
  
  <pre><code>var a = new lines();
  var testline = new aLine();
  var testdot = new aDot();
  
  testdot.height = 10;
  testdot.length = 15;
  testline.insertDot(0,testdot);
  a.addLine(testline);
  a.addLine(testline);
  </code></pre>
  
  <p>Then I want to save the data about lengths and heights. And the structure, to know which dot belongs in which line.</p>
  
  <p>I send that data to the webserver. I think these are the key lines to understand the used approach:</p>
  
  <pre><code>post = "name=" + name + "&amp;tab=" + JSON.stringify(file);
  req.open("POST", "saveFile.php", true);
  req.send(post);
  </code></pre>
  
  <p>The saved file saves exactly what I wanted - the structure and data. But I don't know how to make it become an object again. I am not insisting to use JSON.stringify() method. I would enjoy any approach that would let me save the content without repeatedly saving the methods.</p>