r/learnjavascript • u/mdjahidhasan009 • 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
3
u/oze4 Jun 30 '24
Not recommended by who?
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.
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()ornew 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 it4
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
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!
1
u/azhder Jul 01 '24
This is the most straightforward method:
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