DEV Community

Mrityunjay-Palled
Mrityunjay-Palled

Posted on

Different Ways To Create Objects In JavaScript

There are majorly four ways of creating the objects in JavaScript :

1 .Using Object Literals
2 .Using Factory Functions
3 .Using Object.create()
4 .Using Classes

Using Object Literals:This is the most common way of creating the objects in JavaScript. Object literals are the set of key value pairs separated by comma which are enclosed within the flower braces.

Image description

Using Factory Functions:are the type of function that returns the object

Image description

Using Object.create():It is used to create a new object by using an existing object as prototype for new object

Syntax for Object.create():

Object.create(proto)

Image description

In the above example we can see that firstObj acts as a prototype for secondObj. we can also see that secondObj has access to the myIntro function which is a part of firstObj

Using Classes:Classes are the ES6 syntactic sugar for creating the objects

Image description

Top comments (0)