`new Object` vs `Object` difference

Benjamin Gruenaum benjamingr at gmail.com
Fri Jun 12 20:19:37 UTC 2015


Ok, so I gave this a few hours in the open.

So, I'm looking at the ES5 specification (also checked the current ES draft
which is similar) at the definition of what new Object and Object do. To my
surprise:

- `new Object` describes a whole algorithm of how the object constructor
works - treating what happens with different kinds of values. Basically
calls `ToObject` on non objects - identity on objects and builds on null
and undefined.
 - `Object` has a special first step for null and undefined where it builds
an object and then calls `ToObject` on primitives and identity on objects.

After reading the description a few times - they seem identical. However,
clearly from the spec they do *something* different. For example in Array -
calling new Array is specified as the function call Array(…) is equivalent
to the object creation expression new Array(…) with the same arguments.`

The only difference I've been able to identify with the help of a friend is
that the behaviour can be different on host objects. Where `Object` must
return the same host object and `new Object` _may_ return the same host
object.

I've taken a look at the ES3 specification and it too uses the same
definition so I suspect this is something that has been there for a long
time.

 - Why are `Object` and `new Object` specified differently?
 - If there is no actual reason, can the definition be simplified for the
next version of the spec? (I think simplifying the spec is important and
possibly underrated)

Sorry if I'm missing something obvious.

Originally asked on Stack Overflow -
http://stackoverflow.com/q/30801497/1348195
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150612/ba8675ed/attachment-0001.html>


More information about the es-discuss mailing list