Last Updated: June 09, 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";

26 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 ·

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.

10 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/

8 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/

8 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

6 months ago ·

If you are searching Assignment help.Our experts write high quality assignment.Our professionals have been working on Networking assignments, IT plans, Enterprise software presentations and much more. https://www.allassignmenthelp.com/

6 months ago ·

you can go through Allassignmenthelp.com reviews. It will help you to know about the services, and it will help you to know whether that site is reliable or not. This is a way of understanding the opinion of users, and it helps the website to maintain its functionality and reliability.

6 months ago ·

https://www.courseworksquare.co.uk/law-essay.php is possibly the most competent and credible online writing facility at the moment. They have cautiously recruited a brilliant team of essay writers who ensure 100% originality and authenticity in their content. The service is extremely affordable and has no hidden charges. Place your order now!

5 months ago ·

Will try this thanks for sharing!

5 months ago ·

It’s really helpful for the developer who has javascript issue. I want to become a web developer that's why I read this post for getting information about java. https://duaoazkar.com/

5 months ago ·

It's always best to contact an attorney to go over the accident and determine when you've got the right and if you're in doubt. https://champaignpersonalinjurylawyer.blogspot.com/

5 months ago ·

I have some issue regarding JavaScript but when I found this awesome post I have to resolve my problem in https://almanzilpro.com/ programming.

4 months ago ·

homework helpers Australia

4 months ago ·

Get homework helpers Australia service on students assignment help.com and make your career high. We have experts that offer the world's best homework help in Economics. If you need help, you can mail us on info@studentsassignmenthelp.com or contact us on 7555369184.
Visit:- https://www.studentsassignmenthelp.com/australia/online-homework-helpers/

4 months ago ·

It is safe to say that you need a cheap essay writing assignment? We include an extraordinary group of professional experts to determine all your essay paper composing issues. Our journalists give an essay help assignment. Info more detail please visit here: https://www.uaeassignmenthelp.com/essay-writing-help/

4 months ago ·

ABC Assignment Help comes with a team of renowned assignment writers, proofreaders and editors to assist students in achieving high scores in all the semesters. With a decade of experience, we have emerged as the most trusted brand in online assignment help providing 100% genuine writing and editing services.

4 months ago ·

What a post I found in Google I have the same issue in my development project but I have done resolving after reading your post keep sharing about development. https://www.revealcode.com/coupons/way-codes

3 months ago ·

Much obliged for the online journal stacked with such a variety of data. Ceasing by your site assisted me with getting what I was searching for https://www.makemyassignments.com

3 months ago ·

We offer best assignment help online likes as manage the assignments in schools and colleges. Allassignment help is number 1 assignment help services in US.

2 months ago ·

We look for ways to protect your interests through treatment options, community service alternatives or deferred entry of judgment. Utilizing these and other creative options, we strive to protect your freedom, record and rights. visit https://bennettlawfirmllc.com/.

2 months ago ·

Thanks for nice tips visit my blog. https://www.cdhpl.com

about 2 months ago ·

I had never heard about some of these techniques. Thank you for sharing.

about 1 month ago ·