What is the use of the Object.create() method in JavaScript?

The Object.create() method in JavaScript is used to create a new object with a specified prototype object and properties.

It allows you to create objects that inherit properties and methods from a parent object, also known as a prototype object.

Here are several examples of usages of Object.create():

Creating a Simple Object with a Prototype

Let’s say we want to create a simple object person with a prototype object personProto that has a method sayHello(). We can do this using the Object.create() method, as shown below:

let personProto = {
  sayHello: function () {
    console.log("Hello");
  },
};

let person = Object.create(personProto);

In this code snippet, we first create the prototype object personProto with a sayHello() method that logs "Hello" to the console. We then create a new object person using the Object.create() method and set its prototype object to personProto.

Now, when we call the sayHello() method on the person object, it will inherit the method from the prototype object personProto.

person.sayHello(); // Output: Hello

2. Adding Properties to the Object

We can add properties to the person object using dot notation or bracket notation, just like any other object.

For example:

person.name = "John";
person.age = 30;

We can also set properties of the prototype object personProto, which will be inherited by the person object:

personProto.gender = "Male";

Now, the person object has access to the gender property inherited from the prototype object personProto.

console.log(person.gender); // Output: Male

3. Creating a Chain of Prototype Objects

We can create a chain of prototype objects by setting the prototype object of a prototype object. For example, let’s create a personProto2 object that inherits from personProto.

let personProto2 = Object.create(personProto);
personProto2.sayGoodbye = function () {
  console.log("Goodbye");
};

In this code snippet, we create the personProto2 object using the Object.create() method and set its prototype object to personProto. We also add a sayGoodbye() method to personProto2.

Now, when we create a new object person2 using personProto2 as the prototype object, it will inherit properties and methods from both personProto2 and personProto.

let person2 = Object.create(personProto2);
person2.sayHello(); // Output: Hello
person2.sayGoodbye(); // Output: Goodbye

Using Object.create() with Constructor Functions

We can also use the Object.create() method with constructor functions to create objects that inherit from a parent object.

For example, let’s create a constructor function Person with a prototype object personProto that has a sayHello() method.

function Person(name, age) {
  this.name = name;
  this.age = age;
}

let personProto = {
  sayHello: function () {
    console.log("Hello");
  },
};

Person.prototype = personProto;

In this code snippet, we create the Person constructor function with two arguments name and age. We also create the prototype object personProto with a sayHello() method. Finally, we set the Person.prototype object to personProto.

Now, we can create a new object person using the Person constructor function and set its prototype object to personProto using the Object.create() method.

let person = new Person("John", 30);
let person2 = Object.create(Person.prototype);

person.sayHello(); // Output: Hello
person2.sayHello(); // Output: Hello

In this code snippet, we create a new person object using the Person constructor function with the new keyword and set its properties to "John" and 30. We then create a new person2 object using the Object.create() method and set its prototype object to Person.prototype. Both person and person2 have access to the sayHello() method inherited from the personProto object.

Thank you for reading, and let’s have conversation with each other

Thank you for reading my article. Let’s have conversation on Twitter and LinkedIn by connecting.

Read more: