p5cf5w
Last Updated: May 22, 2019
·
101.6K
· 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";
}

25 Responses
Add your response

28069

Good to practice.

over 1 year ago ·
29554

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

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.

9 months ago ·
31150

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

7 months ago ·
31308

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/

6 months ago ·
31380
31660

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

4 months ago ·
31760

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/

4 months ago ·
31763

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.
http://www.assignmentservicerating.com/a-client-review-on-allassignmenthelp-com/

4 months ago ·
31911

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!

3 months ago ·
31919

Will try this thanks for sharing!
http://www.french-stream.ws

3 months ago ·
31966

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/

3 months ago ·
31983

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/

3 months ago ·
32049

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

3 months ago ·
32197

homework helpers Australia

2 months ago ·
32198

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/

2 months ago ·
32296

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/

about 2 months ago ·
32297

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.
visit
https://www.abcassignmenthelp.com/web-designing-assignment-help

about 2 months ago ·
32409

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

about 1 month ago ·
32721

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

25 days ago ·
32894

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.
https://www.allassignmenthelp.com/

15 days ago ·
32938

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

13 days ago ·
33013

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

7 days ago ·