Create JavaScript Object Using Object.create() method



This JavaScript example shows how to create a JavaScript object using the Object.create() method. 

Create JavaScript Object Using Object.create() method

Object.create() method can be very useful, because it allows you to choose the prototype object for the object you want to create, without having to define a constructor function.
// using Object.create Method
var Employee = {
    firstName : 'Ramesh',
    lastName : 'Fadatare',
    emailId : 'ramesh@gmail.com',
    age : 29,
    getFullName : function (){
        return user.firstName + " " + user.lastName;
    }
} 

var employee1 = Object.create(Employee);

// access object properties
console.log('firstName :',  employee1.firstName);
console.log('lastName :',  employee1.lastName);
console.log('emailId :',  employee1.emailId);
console.log('age :',  employee1.age);
Output:
firstName : Ramesh
lastName : Fadatare
emailId : ramesh@gmail.com
age : 29

Comments