creating objects in javascript with object.assign code example

Example: create new object from existing object javascript

// With ES6 CLASSES
class User {
   constructor(firstName, lastName, dateOfBirth) {
      this.firstName = firstName;
      this.lastName = lastName;
      this.dateOfBirth = dateOfBirth;

      this.getName = function(){
          return "User's name: " + this.firstName + " " + this.lastName;
      }
   }
}

var user001 = new User("John", "Smith", 1985);

// With Object.create()
var user001 = {
   firstName: "John",
   lastName: "Smith",
   dateOfBirth: 1985,
   getName: function(){
      return "User's name: " + this.firstName + " " + this.lastName;
   }
};

var user002 = Object.create(user001);
    
user002.firstName = "Jane";
user002.lastName = "King";
user002.dateOfBirth = 1989;