`new Object` vs `Object` difference

Andrea Giammarchi andrea.giammarchi at gmail.com
Fri Jun 12 21:18:01 UTC 2015


Yep, RegExp too, you are right. The only caveat that might comes to my mind
is dealing with foreigner objects ( through plugins such Java applets and
stuff ) where maybe there is a difference, but I've no memory of any sort
of real-world use case based on the fact that `new` was mandatory.

Indeed if you try even minifiers, you'll notice that code like this
```js
this.bla = new Object(parent.foreignObject);
```
will be minified as
```js
this.bla=Object(parent.foreignObject);
```

On Fri, Jun 12, 2015 at 11:09 PM, Benjamin Gruenaum <benjamingr at gmail.com>
wrote:

> Thanks, Array and Function (as well as RegExp) actually directly specify
> that calling them with and without new produces equivalent results where
> `Object` doesn't and actually has a hole (in what it _may_ do to host
> objects).
>
> It's just baffling that `Object` is defined so differently. I wonder if
> there are old browsers that differ on this, or there is a bigger reason for
> this behaviour.
>
> On Sat, Jun 13, 2015 at 12:05 AM, Andrea Giammarchi <
> andrea.giammarchi at gmail.com> wrote:
>
>> I think historically `Array`, `Function`, and `Object` can be used with
>> or without `new` and the result is exactly the same ... since, about, ever.
>>
>> Agreed if that's actually indeed the case, we could have just one
>> definition for those 3 constructors (not just Object)
>>
>> Regards
>>
>> On Fri, Jun 12, 2015 at 10:19 PM, Benjamin Gruenaum <benjamingr at gmail.com
>> > wrote:
>>
>>> 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
>>>
>>> _______________________________________________
>>> es-discuss mailing list
>>> es-discuss at mozilla.org
>>> https://mail.mozilla.org/listinfo/es-discuss
>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150612/5f47a028/attachment.html>


More information about the es-discuss mailing list