JavaScript

Object.create() in JavaScript

One of the common methods to create an object in JavaScript is the Object.create() method. For this purpose, the method utilizes an already existing object as a prototype. This method returns the new object having the specific properties of the prototype object.

In JavaScript, everything can be an object, e.g., Booleans, Numbers, Strings, etc. The developers tend to use the Object.create() method in the inheritance. This post provides a deep insight into the working and usage of the Object.create() method in JavaScript.

How to Use the Object.create() Method in JavaScript?

In JavaScript, the Object.create() method is a built-in method that is utilized to create a new object. For this purpose, it returns an object having the specific and existing prototype object and properties. The syntax of Object.create() method is provided below:

Syntax
 

Object.create(prototype_object, propertiesObject)

 
The Object.create() method takes two arguments which are enlisted here:

    • prototype_object: Specifies the prototype of the existing object for creating a new object
    • propertiesObject(Optional): Represents the properties to be added to the new object.

Let’s head over to the following examples to practice the Object.create() method.

Example 1: Creating a New Object Using the Object.create() Method

An example is given below for utilizing the built-in Object.create() method of JavaScript.

Code
 

// Example of Object.create() method in JavaScript
const human = {}
const man = Object.create(human, {
  color: {
    value: 'Brown-Asian'
  }
});
console.log(man.color)

 
In the code:

    • A new object, “man” is created by passing the prototype of a human, which is an already existing object.
    • The property “color” is declared for the newly created object.
    • In the end, display the newly created object property of man.color using the console.log() method.


Output


The output shows that the color property of the man object is displayed on the console.

Example 2: Utilizing the Existing Properties of the Object.create() Method

Here, we are using an example to express the property of an existing object into a newly created object in JavaScript. The example code is provided below:

Code
 

// Example of Object.create() method in JavaScript
const school = {  
    Std_Information: function ()  
     {  
      console.log(`Student name is ${this.name}`);
      console.log(`Is he Student? ${this.isStudent}`);  
    }  
  };  
  const me = Object.create(school);  
  me.name = "Minhal"; // "name" is a property set on "me".
  me.isStudent = true; // inherited properties can be overwritten  
  me.Std_Information();

 
In the code:

    • A new object me is created that utilizes the properties of the school object.
    • The properties that are associated with the school object are name and isStudent, which return “Minhal” and “true” values.


Output


The display shows the properties of a new object me that is already present in the existing object school. In this way, the Object.create() method retrieves the specified prototype of the object in JavaScript.

Conclusion

JavaScript extracts the properties of existing objects by creating new objects with the Object.create() method. Using this method, users can retrieve the specified prototype of objects and properties. This post specifies the overview of the Object.create() method. Moreover, two examples are provided to understand the concept of this method in JavaScript.

About the author

Syed Minhal Abbas

I hold a master's degree in computer science and work as an academic researcher. I am eager to read about new technologies and share them with the rest of the world.