What are the possible ways to create objects in JavaScript
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There are many ways to create objects in javascript as below
Object constructor:
The simplest way to create an empty object is using the Object constructor. Currently this approach is not recommended.
Object’s create method:
The create method of Object creates a new object by passing the prototype object as a parameter
Object literal syntax:
The object literal syntax (or object initializer), is a comma-separated set of name-value pairs wrapped in curly braces.
Note: This is an easiest way to create an object
Function constructor:
Create any function and apply the new operator to create object instances,
Function constructor with prototype:
This is similar to function constructor but it uses prototype for their properties and methods,
This is equivalent to an instance created with an object create method with a function prototype and then call that function with an instance and parameters as arguments.
(OR)
ES6 Class syntax:
ES6 introduces class feature to create the objects
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.