create new object from existing object javascript code example
Example 1: javascript construct new object
person = new Object();
Example 2: create new object from existing object javascript
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);
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;
Example 3: create nodejs new object
function Person(fname, lname) {
this.firstName = fname;
this.lastName = lname;
}