title: Copying AJAX JSON object into existing Object
body: <p>I have several Javascript prototypes. Initially, instances will have only ID's filled in, with some generic place holder information for other data. I then send a message to the server with the ID and the object type (using jQuery's AJAX function) and the server returns a JSON object with all the missing information (but no ID). The variables in the returned object have the exact same name as those in the existing object.</p>
  
  <p>What's the easiest way to transfer this into the existing empty object? I've come up with a few alternatives</p>
  
  <ul>
  <li>set the object equal to the returned object, then copy in the id (loses prototype functions?)</li>
  <li>create a function for each object that takes an object with identical structure and copies the data</li>
  <li>loop through the key-value pairs of the JSON object to and copy them to existing object</li>
  </ul>
  
  <p>If I use the third option, is this the correct way to do that? :</p>
  
  <pre><code>for (var key in json) {
      if (object.hasOwnProperty(key)) {
          object[key] = json[key];
      }
  }
  </code></pre>
  
  <p>assuming <code>json</code> is the returned object and <code>object</code> is the existing object.</p>