p5cf5w
Last Updated: March 20, 2019
·
74.58K
· 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";
}

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

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

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

4 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

about 2 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/

about 1 month 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/

about 1 month 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!

28 days ago ·
31919

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

28 days 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/

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

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

about 19 hours ago ·
32197

homework helpers Australia

about 1 hour 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/

about 1 hour ago ·