title: Copy Javascript "Class" Prototype to an Object
body: <p>I have a created a <em>class</em> <code>User</code>. It has some properties like: name, id, password, etc.</p>
  
  <p>I've set up a constructor like:</p>
  
  <pre><code>function User(name1, email, password) {
    this.name = name1;
    this.id = __GUID();
    this.type = __TYPE;
    this.addNewEmail(email);
    this.password = (new Password).createFromRawPassword(password).crypt;
  }
  </code></pre>
  
  <p>And some prototype functions like:</p>
  
  <pre><code>User.prototype.save = function(cb, callback) {
    return cb.bucket.upsert(this.id, this, function(err, res) {
      if (callback == null) {
        return callback(err, res);
      }
    });
  };
  
  User.prototype.addNewEmail = function(email) {
    this.emails = this.emails || [];
    return this.emails.push(email);
  };
  </code></pre>
  
  <p>This works pretty nice for me, and let's me store my object as serialized JSON (in a couchbase database with NodeJS) without including in the object the functions.</p>
  
  <p>The problem comes when I get the object from DB. It comes with all the properties, as stored, but I need to add back the functions. I have tried to use some <code>extend</code> functions for mixins that I found, but they add the functions as ownProperties, and therefore, next time I save the updated record to DB, I get the functions saved.</p>
  
  <p><strong>How can I turn the received object into a object of type User again?</strong> To get the required methods of the class appearing also on the instance.</p>