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

4

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

I was curious, and I went to learn. I understood the operation, although I have my doubts about the usefulness of them, since my knowledge is not yet very great. But until then, that's fine.

Then I came across new Object() . I got confused. I thought that if it exists, there should be a difference rather than declare with a simple {} , but apparently it does not exist.

If there is no difference between making one and doing another, what is the utility of the longer syntax? What was the reason this builder was created?

    
asked by anonymous 13.09.2018 / 20:09

1 answer

4

The question should be "why should not it have new Object()? ".

Contrary to what many people think (even if they say they do not think so), the construction of language follows a logic, it has a grammar.

This is good for the programmer (actually) to learn more easily and 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 decorate words and phrases, but many will evolve knowledge naturally and progressively, always understanding what they are talking about, which makes them understand the whole, the concept and not just the words, after all if this is not a form of functional illiteracy), except exception rules necessary for some reason constructs follow a logic.

It's also good for building a compiler that needs to handle everything consistently and linearly, that is, you do not have to be inventing out-of-normal rules unnecessarily.

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

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

It should not, but someone could override the% default% in some abnormal situation. And you have difference for normal use . If there was nothing different then one could argue not to use something that is clearly worse (and apparently worse even in performance, at least in some implementation and specific situation ), but it would be ideology.

Viewing an answer here that says both are equivalent, it means being semantically equivalent, not that it gives the same result (this even gives) and the same way.

It's a question of symmetry. The construction of languages, especially programming, need to be pragmatic, you earned it "for free" and to not have there will be a cost, so leave it there. Will anyone find a reason to use, or arrange an optimization or function that does not change the semantics of its default and legacy use but providing some advantage?

There is pragmatism. To withdraw it would be ideology.

    
13.09.2018 / 20:45