Loading a javascript object from the server into an existing javascript object <p>I am probably not the first one doing this, but I can't find or figure out how to do it.</p> <p>In my javascript I have an object with 5 properties, which are all object withg data properties and functions.</p> <p>So it looks like this</p> <pre><code>function Maindata() { var obj = this; this.id = null; this.profile= new Profile(); this.company= new Company(); //and more this.getid = function(){ return obj.id; }; } </code></pre> <p>and the <code>profile</code> and <code>company</code> object also has data and functions.</p> <p>Now I get an object from the server, which is the same object, but only the data, and not the functions.</p> <p>Now I want to project all of the properties of the retrieved object from the server to my JavaScript object.</p> <p>I've read to use jQuery Extend, but that doesn't work for me, because it only preserves the functions on the root (Maindata) object, but not on the sub-objects.</p> <p>How can I load all the data from the subobjects too?</p> <p>Do I have to $extend the subobjects one by one, like this:</p> <pre><code>$.extend(true, maindata.profile, result.profile); $.extend(true, maindata.company, result.company); </code></pre> <p>or is there another solution?</p>