# The behavior of new Object(value) - are implementations wrong?

**URL:** <https://es.discourse.group/t/the-behavior-of-new-object-value-are-implementations-wrong/789>\
**Category:** I have questions\
**Created:** [May 13, 2021, 9:48am UTC](https://es.discourse.group/t/the-behavior-of-new-object-value-are-implementations-wrong/789 "2021-05-13T09:48:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![iliakan](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/iliakan/32/820_2.png) [@iliakan](https://es.discourse.group/u/iliakan)\
**Post date:** [May 13, 2021, 9:48am UTC](https://es.discourse.group/t/the-behavior-of-new-object-value-are-implementations-wrong/789/1 "2021-05-13T09:48:39Z")

</div>

Consider the behavior of `"new Object(value)"`.

According to the spec [ECMAScript® 2022 Language&nbsp;Specification](https://tc39.es/ecma262/#sec-object-constructor), the `Object` constructor:

- creates a new [ordinary object](https://tc39.es/ecma262/#ordinary-object) when called as a [constructor](https://tc39.es/ecma262/#constructor).

So, the call `new Object(value)` should create an empty ordinary object, right? There's no word here about arguments of the constructor. (And I'm not talking about function-style call `Object(value)`).

Although, in implementations, e.g. Chrome and Firefox, a call `new Object('123')` returns a bogus "String" object. So that e.g. `new Object('123') === '1'`.

Why is that? Why not an empty object? Or maybe I just misunderstand the spec? ;)

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [May 13, 2021, 11:00am UTC](https://es.discourse.group/t/the-behavior-of-new-object-value-are-implementations-wrong/789/2 "2021-05-13T11:00:13Z")

</div>

20.1.1.1 specifies the behavior of the function when called with one argument. Which will reach `toObject(stringValue)` for your example.

[https://tc39.es/ecma262/#sec-object-value](https://tc39.es/ecma262/#sec-object-value)

An 'ordinary object' is an object that is not exotic. But `new Object([1])` does return an Array, which is an exotic object.

This may be that constructors can always return a different object from the one they create. So the Object constructor may always _create_ a ordinary object but it does not necessarily _return_ that object.

```javascript
class C {
  constructor() { return new String() }
}

new C;

```

The above call to the C constructor will create a new instance of C but a new String will be returned

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/senocular/32/642_2.png) [@senocular](https://es.discourse.group/u/senocular)\
**Post date:** [May 13, 2021, 11:21am UTC](https://es.discourse.group/t/the-behavior-of-new-object-value-are-implementations-wrong/789/3 "2021-05-13T11:21:15Z")

</div>

> [@iliakan](#):
>
> So, the call `new Object(value)` should create an empty ordinary object, right?

Yes. And it does this. Keep in mind that ordinary objects consist of more than just basic Object objects. They're all objects that aren't exotic objects (as: "An exotic object is an object that is not an ordinary object").

> [@iliakan](#):
>
> There's no word here about arguments of the constructor. (And I'm not talking about function-style call `Object(value)`).

This is handled in [2.1.1.1](https://tc39.es/ecma262/#sec-object-value). The list in 2.1.1 is more of a summary. 2.1.1.1 refers to how Object behaves when called, both with and without new as determined by the presence of `NewTarget`:

> When the `Object` function is called with optional argument value, the following steps are taken:
> 
> 1. If NewTarget is neither undefined nor the active function, then  
> a. Return ? [OrdinaryCreateFromConstructor](https://tc39.es/ecma262/#sec-ordinarycreatefromconstructor)(NewTarget, "%Object.prototype%").
> 2. If value is undefined or null, return ! [OrdinaryObjectCreate](https://tc39.es/ecma262/#sec-ordinaryobjectcreate)([%Object.prototype%](https://tc39.es/ecma262/#sec-properties-of-the-object-prototype-object)).
> 3. Return ! [ToObject](https://tc39.es/ecma262/#sec-toobject)(value).

The first step accounts for when being used with super() which always creates a new Object object. The second step is when there is no value passed (or its passed but null or undefined). This always creates a new Object object. Finally, when value is passed, it runs it through ToObject - this both with and without using `new`. Ultimately there is no difference between:

```js
Object('123') // String {"123"}
new Object('123') // String {"123"}

```

You can compare this to something like the Boolean constructor ([20.3.1.1](https://tc39.es/ecma262/#sec-boolean-constructor-boolean-value)) where delineation between constructor and not is a little more clear with an explicit check for NewTarget being undefined.

> When `Boolean` is called with argument value, the following steps are taken:
> 
> 1. Let b be ! [ToBoolean](https://tc39.es/ecma262/#sec-toboolean)(value).
> 2. If NewTarget is undefined, return b.
> 3. Let O be ? [OrdinaryCreateFromConstructor](https://tc39.es/ecma262/#sec-ordinarycreatefromconstructor)(NewTarget, "%Boolean.prototype%", « [[BooleanData]] »).
> 4. Set O.[[BooleanData]] to b.
> 5. Return O.

Remember, the Object constructor doesn't check for "If NewTarget is undefined" which indicates no presence of `new`. It instead has a check for "If NewTarget is neither undefined nor the active function" for checking for super, which means everything else can still be a normal call or a new call.

> [@iliakan](#):
>
> So that e.g. `new Object('123') === '1'`.

Not sure what's going on here. This shouldn't be the case

```js
new Object('123') === '1' // false

```

---

<div class="post-metadata">

**Author:** ![claudepache](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/claudepache/32/236_2.png) [@claudepache](https://es.discourse.group/u/claudepache)\
**Post date:** [May 13, 2021, 1:12pm UTC](https://es.discourse.group/t/the-behavior-of-new-object-value-are-implementations-wrong/789/4 "2021-05-13T13:12:36Z")

</div>

Hi,

> [@iliakan](#):
>
> - creates a new [ordinary object](https://tc39.es/ecma262/#ordinary-object) when called as a [constructor](https://tc39.es/ecma262/#constructor).

In case the value passed as argument is a non-object, that phrase is correct, although it is not a complete description (the bogus String objects are indeed “ordinary objects”).

However, there is a contradiction between the phrase you have cited and the step 3 of the algorithm just below, in case the argument is itself an object; in that case no object is created. (And please, do not argue that such an object is created but not returned.)

Generally, the description text preceding the algorithm is informative, and the algorithm is normative, so that there is no bug in implementations (as they follow the algorithm), but there is a bug in the spec.

I’ve open [Issue 2407](https://github.com/tc39/ecma262/issues/2407) in order to track the spec bug.

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [May 13, 2021, 2:19pm UTC](https://es.discourse.group/t/the-behavior-of-new-object-value-are-implementations-wrong/789/5 "2021-05-13T14:19:04Z")

</div>

> [@claudepache](#):
>
> And please, do not argue that such an object is created but not returned

Agreed! I was wrong above, the spec text for the Object constructor definitely does not create a new object in all cases.

---

<div class="post-metadata">

**Author:** ![iliakan](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/iliakan/32/820_2.png) [@iliakan](https://es.discourse.group/u/iliakan)\
**Post date:** [May 14, 2021, 7:51pm UTC](https://es.discourse.group/t/the-behavior-of-new-object-value-are-implementations-wrong/789/6 "2021-05-14T19:51:11Z")

</div>

So, to summarize, `new Object(value)` is the same as `Object(value)`?

Or else what does it do?

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [May 14, 2021, 9:10pm UTC](https://es.discourse.group/t/the-behavior-of-new-object-value-are-implementations-wrong/789/7 "2021-05-14T21:10:01Z")

</div>

Yes it looks like there is no observable difference between the two
