Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Why are there different ways of creating objects in javascript?

Why are they so many ways to create an object in js and in what ways do these differ, do they have pros and cons, do they have the same different prototypes and is there any other info I should know about them?

var obj= new Object; var obj2= Object.create(Object); var obj3= {};

obj3.constructor would return Object(), while obj2.constructor would return a named function called Function, why is there two native objects that create an object as opposed to one?

1 Answer

var obj = new Object(), // Creates a new instance of an Object
    obj2 = Object.create(null), // Creates an empty object " {} "
    obj3 = {}; // Shorthand of obj2

I hope this helps.

Its not really answering the question, why there are 3 different ways...

Avraham Shukron there are multiple ways of creating objects, among other things, in JavaScript due to the evolution of the language. These examples all serve the same purpose however the first two are no longer considered best practice.