Creating object with Object.create() method

The Object.create() method creates a new object, using an existing object as the prototype of the newly created object.

Example: This example shows the implementation of the above-explained approach.

javascript
const coder = {
    isStudying : false,
    printIntroduction : function(){
        console.log(`My name is ${this.name}. Am I studying?: ${this.isStudying}`);
    }
};
const me = Object.create(coder);
me.name = 'Mukul';
me.isStudying = true;
me.printIntroduction();

Output
My name is Mukul. Am I studying?: true

Creating objects in JavaScript

A JavaScript object is a data structure containing properties and methods. Objects can be created using object literals, constructors, or classes. Properties are defined with key-value pairs, and methods are functions defined within the object, enabling encapsulation and organization of data and behavior.

These are the following 4 ways:

Table of Content

  • Creating object with a constructor
  • Using object literals
  • Creating object with Object.create() method
  • Using es6 classes

Similar Reads

Creating object with a constructor

One of the easiest ways to instantiate an object is in JavaScript. Constructor is nothing but a function and with the help of a new keyword, the constructor function allows to creation of multiple objects of the same flavor as shown below:...

Using object literals

Literals are smaller and simpler ways to define objects. We simply define the property and values inside curly braces as shown below....

Creating object with Object.create() method

The Object.create() method creates a new object, using an existing object as the prototype of the newly created object....

Using es6 classes

ES6 supports class concept like any other Statically typed or object oriented language. So, object can be created out of a class in javascript as well....