JavaScript Object and multiple ways to create it

What is Object?

JavaScript defines 5 types of primitive data types:

Object Creation

1. Using the Object() constructor:

let obj = new Object();

2. Using Object.create() method:

let obj = Object.create(null);

3. Using the bracket’s syntactig sugar:

let obj = {};

4. Using a function constructor:

let ObjCreate = function(name) {
this.name = name
}
let obj = new ObjCreate("abhayjain13");
function f {};

new f(a, b, c);

Would be equivalent to:

// Create a new instance using f's prototype.
let newInstance = Object.create(f.prototype)
let result;

// Call the function
result = f.call(newInstance, a, b, c),

// If the result is a non-null object, use it, otherwise use the new instance.
result && typeof result === 'object' ? result : newInstance

5. Using the function constructor + prototype:

function myObj(){};
myObj.prototype.name = "abhayjain13";
let obj = new myObj();

6. Using ES6 class syntax:

class myObject  {
constructor(name) {
this.name = name;
}
}
let obj = new myObject("abhayjain13");

7. Singleton pattern:

let obj = new function(){
this.name = "abhayjain13";
}

--

--

--

Developer with 3 yrs of industrial experience in developing scalable web applications.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Mr Frontend weekly sharing #52

Minimum Swap 2 Javascript

Returning To AngularJS (After Angular 2+)

Solve the Secret Santa Mix-Match with Google Sheets and Apps Script

Guide to express middlewares

Email verification using NODE js

Introduction to ReactJs

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abhay Jain

Abhay Jain

Developer with 3 yrs of industrial experience in developing scalable web applications.

More from Medium

Special Concepts about JavaScript & MongoDB

What is a framework?

A quick guide for passing props using navigation and UseLocation hook in react-router-dom

Conditional (ternary) operator in Javascript