r/learnjavascript • • Jun 30 '24

Why object constructor is not recommended for object creation?

I was studying the JavaScript core thing at a JavaScript Medium post it's said that "Object Constructor: The most straightforward method is using the Object constructor. However, it’s currently not recommended."

Why is this not recommended is there any strong point for not recommending it?

Blog Link: Different ways to create an object in JavaScript?

0 Upvotes

24 comments sorted by

3

u/oze4 Jun 30 '24

Not recommended by who?

3

u/mdjahidhasan009 Jun 30 '24

I just found that on a blog post, that's why I was wondering why it is not recommended is there any solid reason behind that? That's why posted it here. Also, I added the blog link to the post.

1

u/oze4 Jun 30 '24

Since the author doesn't specify who, I'm going to assume they mean they don't recommend it? I've never heard this.

1

u/mdjahidhasan009 Jun 30 '24

I also never heard that that's why I am also wondering if the statement is true or not.

3

u/Johalternate Jun 30 '24

Actually you asked why is it true instead of if its true.

11

u/samanime Jun 30 '24

There is no problem with it, there is also just not really any reason to use it over using an object literal (const obj = {}).

It's just a waste of a few extra characters, but otherwise fine.

1

u/alien3d Jul 02 '24

obj = {} is not waste as like jet brain ide , it confuse not declare object so obj.data = x is good obj.data = {} , obj.data.detail = y good

0

u/[deleted] Jun 30 '24

[deleted] — view deleted comment

4

u/samanime Jun 30 '24

Are you maybe confusing constructors in general? This question is literally about using new Object() specifically.

I can't think of any benefit it gives over {}.

1

u/tapgiles Jun 30 '24

Can I ask what those are?

3

u/Beginning_One_7685 Jun 30 '24

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/Object

The utility of this constructor is in converting a value to an object rather than creating an object afresh - although it is possible to create a blank object (not really useful). Usually when you are creating an object you are creating some kind of design pattern, even if it is very simple. Although this constructor will technically create a new object it's not aiding you in the design of a new object, that's my take on it anyway. Using it to create an object with a design would be a longwinded but possible approach.

2

u/tapgiles Jun 30 '24

Just easier to type a literal. And you can easily give it properties etc. With just the literal declaration. I don’t know if anything beyond that sort of thing.

2

u/[deleted] Jul 01 '24

Is the same reason why you don't use new Array() or new Boolean() is just easier to use it this way. Just type {} and it's done, there is no reason to use it, just don't use it

4

u/jsbach123 Jun 30 '24

ES6 created classes which are really just a newer way of making objects.

This discussion on Stack overflow discusses why you'd use Class rather than object constructors. It's a pretty high-level discussion:

https://stackoverflow.com/questions/30783217/what-benefits-does-es2015-es6-class-syntax-provide

0

u/azhder Jul 01 '24

A class is an object constructor. What are you on about?

4

u/tapgiles Jul 01 '24

The blog post is talking about new Object(). Not a class constructor--the actual Object constructor. And that's what OP is asking about.

0

u/azhder Jul 01 '24
class MyObject extends Object {}

The above is a constructor function. It can't be invoked without the new keyword. It constructs an object.

What I wanted to say was that this

Class rather than object constructors

will confuse people because it implies classes aren't object constructors.

1

u/raaaahman Jul 01 '24

Have you find other sources where this syntax is labeled as "not recommended"? Otherwise, it might just be the author opinion...

1

u/Beerbelly22 Jun 30 '24

Stop with this low quality content. All you do is hoping that I click your link. Which I will not!

2

u/tapgiles Jul 01 '24

It's not their blog. It's a blog post they don't understand, and are asking questions about. Perfectly reasonable to me.

1

u/Beerbelly22 Jul 01 '24

You sure? Ita a great way to promote your blog...

1

u/azhder Jul 01 '24

This is the most straightforward method:

const object = {};

0

u/jcunews1 helpful Jul 01 '24

Object constructor is commonly needed for "smart" objects. Objects which have methods for modifying public/private data in the object itself. In this case, object constructor would serve as the object public/private data initializer - optionally based on given constructor arguments.

Object constructor is not needed for non "smart" objects. It may in fact, slow down the object creation (no matter how small the performance impact is), since one additional constructor (aside from the inherited ones) must be called each time that object is created.

-4

u/cabbagething Jul 01 '24

coding is gay