What is the use / reason for the existence of new Object();?

Asked

Viewed 127 times

7

While I was studying a little bit more about objects, I came across things like new String , new Number, etc..

I got curious, and I went to learn. I understood the functioning, despite having my doubts about the usefulness of them, have seen that my knowledge is still not very great. But so far, so good.

So I came across the new Object(). I was confused. I thought that if it exists, there must be a difference instead of declaring with a simple {}, but apparently there is no.

If there are no differences between making one and making another, what is the use of the longer syntax? Why was this constructor created?

1 answer

7


The question should be "why shouldn’t there be new Object()?".

Contrary to what many people think (even if you say you don’t think so), the construction of language follows a logic, has a grammar.

This is good for the programmer (really) to learn more easily and be able to use intuition (very carefully) to understand everything how it works, and it can be easier to use. It is the same as learning a normal language (I know that many only memorize words and phrases, but many are evolving knowledge in a natural and progressive way, always understanding what you are talking about, what makes it understand the whole, the concept and not just the words, after all if it is not so is a form of functional illiteracy), unless necessary exception rules for some reason constructions follow a logic.

It is also good for building the compiler who needs to treat everything consistently and linearly, IE, no need to keep inventing rules out of the ordinary without need.

So if you can use the constructor for several specific objects for other types, why shouldn’t you be able to use it for the generic object? Why make the exception of this? Every exception needs to be justified. They created the constructor, not the constructor Object.

Remembering that you can (or not) pass a parameter to this constructor function. In other types it is quite common that it has parameters and makes much more sense. Indeed, the literal of other types is just new <Tipo>([param]). Again, why hide the constructor of a specific type? So they are not always identical.

It shouldn’t, but someone could override the Object pattern in some abnormal situation. And has difference for normal use. If there was nothing different, someone could argue not to use something that is clearly worse (and apparently is worse even in performance, at least in some implementation and specific situation), but it would be ideology.

Seeing an answer here which says that both are equivalent, means to be semantically equivalent, not to give the same result (this even gives) and in the same way.

It’s a question of symmetry. Building languages, especially programming, need to be pragmatic, you won it "for free" and there won’t be a cost, so better leave there. Will someone find a reason to use, or arrange an optimization or function that does not change the semantics of your default use and legacy but providing some advantage?

It exists for pragmatism. To withdraw it would be ideology.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.