DEV Community

Cover image for Creating objects in JavaScript

Creating objects in JavaScript

hebashakeel profile image hebaShakeel ・2 min read

There are several ways one can create objects in JavaScript. I have mentioned most of the ways to do so.

1. Object Constructor

This is the simplest way to create an empty object.

In the below example function Person( ) is an object constructor function. Objects of the same type are created by calling the constructor using the new keyword.

Alt Text

2. Object.create method

The create method of Object creates a new object by passing the prototype object as a parameter. The newly created object will inherit all the prototype object properties.

Alt Text

3. Object Literal Syntax

The object literal syntax is equivalent to create method when it passes null as parameter.

Alt Text

4. Function Constructor

The Function constructor creates a new Function object. The Function constructor creates functions which execute in the global scope only.

Alt Text

5. Function constructor with prototype

This is similar to function constructor but it uses prototype for their properties and methods

Alt Text

6. ES6 Class Syntax

Alt Text

7. Singleton pattern

A Singleton is an object which can only be instantiated one time. Repeated calls to its constructor return the same instance and this way one can ensure that they don't accidentally create multiple instances.

Alt Text

Discussion (0)

Editor guide