Playing with built-in JavaScript objects and constructors, I noticed something a little odd.
Sometimes, it's possible to get new objects by calling a constructor without new
. For example:
> new Array(1,2,3,4)
[1, 2, 3, 4]
> Array(1,2,3,4)
[1, 2, 3, 4]
But sometimes this doesn't work:
> Date()
"Thu Jun 05 2014 00:28:10 GMT-0600 (CST)"
> new Date()
Date 2014-06-05T06:28:10.876Z
Is the behavior of non-new constructor built-in functions defined anywhere in the ECMAScript specification? Note that this behavior is actually useful; I can make a non-sparse copy of an array by calling Array.apply(arr)
, but I'd only feel comfortable doing that if it were cross-platform.
The behaviour of a native method depends on the EcmaScript specification.
For
Date
the spec says :and for
Array
the spec saysSo how it works with or without the
new
keyword is completely dependant on what method you're using, and what the spec says should happen when called without the new keyword.For instance, the Math object is different again
Yes, ECMA-262 (I am using 5.1 Edition for reference) does define how should object constructors behave when called with or without the
new
keyword.For
Array
:15.4.1 The Array Constructor Called as a Function:
15.4.2 The Array Constructor:
For
Date
:15.9.2 The Date Constructor Called as a Function:
15.9.3 The Date Constructor: