Last Updated: January 19, 2019
· fr0gs

Different ways of creating an Object in javascript

Using the Object() constructor:

var d = new Object();

This is the simplest way to create an empty object. I believe it is now discouraged.

Using Object.create() method:

var a = Object.create(null);

This method creates a new object extending the prototype object passed as a parameter.

Using the bracket's syntactig sugar:

var b = {};

This is equivalent to Object.create(null) method, using a null prototype as an argument.

Using a function constructor

var Obj = function(name) {
  this.name = name
var c = new Obj("hello"); 

What the new operator does is call a function and setting this of the function to a fresh new Object, and binding the prototype of that new Object to the function's prototype. As is:

function f {};

new f(a, b, c);

Would be equivalent to: 

// Create a new instance using f's prototype.
var newInstance = Object.create(f.prototype)
var 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

Using the function constructor + prototype:

function myObj(){};
myObj.prototype.name = "hello";
var k = new myObj();

Using ES6 class syntax:

class myObject  {
  constructor(name) {
    this.name = name;
var e = new myObject("hello");

Singleton pattern:

var l = new function(){
  this.name = "hello";

9 Responses
Add your response


Good to practice.

over 1 year ago ·

var a = Object.create(null); is not the same as var a = {};
var a = Object.create(null); sets the prototype of a as null where as
var a = {}; sets the prototype of a as the Object object

over 1 year ago ·

Thanks for this informative article if you were the user of windows 10 and getting trouble with your USB port then from here https://fixforwindows.com/usb you will learn to troubleshoot all of your pc issue in easiest way.

5 months ago ·

A prevalent all task Help survey offered by this site with the benefit of online help with high capability level in view of its most recent research and data by https://www.assignmentdone.co.uk proficient audits journalists. Wide scopes of subjects are secured with particular journalists for each subject.

4 months ago ·

SEO Services. If Search Engine Optimization (SEO) isn't a part of your marketing mix it should be. http://msoftsys.com/services/seo/

2 months ago ·

Hotmail login email: Sign into Outlook.com, Hotmail.com using a Windows, Mac OS web browser or mobile device. https://hotmail-i.com/hotmail-login/

about 2 months ago ·

I get it relies upon what you need. For straightforward articles, I surmise you could utilize the second techniques.https://www.assignmentsquare.co.uk/ At the point when your articles become bigger and you're anticipating utilizing comparative items, I surmise the principal technique would be better. That way you can likewise broaden it utilizing models

3 days ago ·