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.