43

So, I'm looking at the ES5 specification 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.`

So - what is the difference between new Object and Object? Why were they specified differently?

For ease - here's a link to the spec.

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • JS room people (namely Jan Drovak) guessed it has to do with host objects but I couldn't figure out one that actually made a difference. – Benjamin Gruenbaum Jun 12 '15 at 11:03
  • `new Object` is implementation-defined on host objects. I'd still like to see an example of when it is not an identity. – John Dvorak Jun 12 '15 at 11:05
  • 14
    Is this going to be one of those famous questions? –  Jun 12 '15 at 11:09
  • `When Object is called as part of a new expression, it is a constructor that may create an object.` which concerns `If the value is a host object, then actions are taken and a result is returned in an implementation-dependent manner that may depend on the host object.` and this is implementation dependant. `ToObject` - `Object: The result is the input argument (no conversion).` This seems to be the primary difference. Why? Probably to allow the `implementation dependant` part – Xotic750 Jun 12 '15 at 11:44
  • Here are the [tests](https://code.google.com/p/sputniktests/source/browse/trunk/tests/Conformance/15_Native_ECMA_Script_Objects/?r=81#15_Native_ECMA_Script_Objects%2F15.2_Object_Objects%2F15.2.1_The_Object_Constructor_Called_as_a_Function) used for that part of the spec. – quw Jun 12 '15 at 13:03
  • Not an answer , [**but worth** to look at](http://i.imgur.com/uMciWWR.png) – Royi Namir Jun 12 '15 at 14:51
  • @RoyiNamir JSLint is [known for being overly opinionated](http://stackoverflow.com/a/6803574/129032), so I'm not sure that's admissible evidence. ;) – ssube Jun 12 '15 at 15:09
  • @ssube :-) never said it is an evidence , just worth to mention/know/wonder :-) – Royi Namir Jun 12 '15 at 15:10
  • 1
    Why not ask on https://esdiscuss.org/? – Felix Kling Jun 12 '15 at 15:29
  • @FelixKling because as a reader I hate when people do that without researching first - I figured asking here would be a logical third step (after reading the spec again myself and googling). If I don't get an answer here in a day I'll ask there. Although, to be fair I'm not very optimistic by now since the old ES3 spec has the same definitions. – Benjamin Gruenbaum Jun 12 '15 at 15:32
  • Ok, I give up https://esdiscuss.org/topic/new-object-vs-object-difference – Benjamin Gruenbaum Jun 12 '15 at 20:21
  • 8 hours is too short , to give up :-) – Royi Namir Jun 14 '15 at 07:06
  • So now you've had a couple of days and asked the question on `esdiscuss`, what is the answer to `what is the difference between new Object and Object? Why were they specified differently?`? – Xotic750 Jun 17 '15 at 09:22
  • Sounds like you are hoping for Brendan Eich to answer. :) – Xotic750 Jun 17 '15 at 09:35
  • @Xotic750 Brendan Eich has already probably seen it as he reads everything on https://esdiscuss.org/topic/new-object-vs-object-difference - he probably doesn't remember either. – Benjamin Gruenbaum Jun 17 '15 at 09:36

1 Answers1

16

Object(window) will never clone window but new Object(window) might. All current -- potentially all known -- implementations just return the same reference, although the spec allows for implementation-defined behavior.

The steps for 15.2.1.1 say:

  1. If value is null, undefined or not supplied, create and return a new Object object exactly as if the standard built-in Object constructor had been called with the same arguments
  2. Return ToObject(value).

The definition of ToObject (9.9) lists a few types that will be caught by step 1 (in table 14), but for Object has a very simple definition:

The result is the input argument (no conversion).

It explicitly states that the input argument will be returned as-is, so they should be equal references (===).

The definition for new Object (15.2.2.1) has a similar chain of type-checks in step 1, but the step for objects (1.a) is:

i. If the value is a native ECMAScript object, do not create a new object but simply return value.

ii. If the value is a host object, then actions are taken and a result is returned in an implementation-dependent manner that may depend on the host object.

That is, for any host object foo, the call Object(foo) must === foo but new Object(foo) may === foo.

Host objects are defined in 4.3.8 to be

object supplied by the host environment to complete the execution environment of ECMAScript.

This answer lists a few host objects to include window, history, etc. Running those through new Object(foo) should (but doesn't have to) return a different object.

In any case but passing a host object, new Object(foo) seems to be a more complicated chain that defers to ToObject in much the same way as Object(foo).

Unfortunately, 15.2.2.1.1.a.ii states that the "result is returned in an implementation-dependent manner" and has no specifics as to the "actions [that] are taken" and it appears that Chrome will return the same object (equal references) for all of the listed "host objects."

Using this script to check:

var objects = [
  /* Native objects */
  'Object', 'Date', 'Math', 'parseInt', 'eval',
  /* Host objects */
  'window', 'document', 'location', 'history', 'XMLHttpRequest', 'setTimeout'
];

function getDefinedReference(name) {
  if (eval('typeof ' + name) !== 'undefined') {
    return eval(name);
  } else {
    throw new Error('' + name + ' is not defined.');
  }
}

function checkIdentity(name) {
  try {
    var ref = getDefinedReference(name);
    var no = new Object(ref);
    var o = Object(ref);

    console.log(name, ref === no, ref === o, no === o);

    if (ref === o && no !== o) {
      // Make sure ref === Object(ref) but not new Object(ref)
      console.log(name, 'returns different references.');
    }
  } catch (e) {
    console.warn(e);
  }
}

objects.forEach(checkIdentity);

if (typeof window !== 'undefined') {
  for (var f in window) {
    checkIdentity(f);
  }
}

doesn't find any objects where Object and new Object behave differently. @Xotic750 seems to be right that it can be implementation-dependent, but nobody is using it.

Community
  • 1
  • 1
ssube
  • 47,010
  • 7
  • 103
  • 140