new無しで基本型のコンストラクタを呼んだ時の挙動

Closure Compilerがnew無しのArray()を使うのを見て興味を持ったので調べました。大雑把にこうなります。

# ES5での仕様

| 型名 | 挙動 |
|------|----|
|`Object()` | 引数をオブジェクトに変換 (ボクシング含む) |
|`String()`<br>`Boolean()`<br>`Number()` | 引数をそのプリミティブに変換 |
|`Function()`<br>`Array()`<br>`RegExp()`<br>`Error()` | newがあるときと同じ |
|`Date()` | <code>new Date().toString()</code>と同様<br />(引数は無視される)|

`Date()`だけ挙動が不思議ですね。

`String(x)`は`x + ""`の代わり、`Boolean(x)`は`!!x`の代わり、`Number(x)`は`+x`の代わりとして`map`などと組み合わせると便利そうです。

以下[ECMAScript 5の仕様書](http://www.ecma-international.org/ecma-262/5.1/)より抜粋します。

## `Object`について

>15.2.1 The Object Constructor Called as a Function
>When Object is called as a function rather than as a constructor, it performs a type conversion.
>
>15.2.1.1 Object ( [ value ] )
>When the Object function is called with no arguments or with one argument value, the following steps are
>taken:
>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 (15.2.2.1).
>2. Return ToObject(value).

## `Function`について

>15.3.1 The Function Constructor Called as a Function
>When Function is called as a function rather than as a constructor, it creates and initialises a new Function
>object. Thus the function call Function(…) is equivalent to the object creation expression new
>Function(…) with the same arguments.

## `Array`について

>15.4.1 The Array Constructor Called as a Function
>When Array is called as a function rather than as a constructor, it creates and initialises a new Array object.
>Thus the function call Array(…) is equivalent to the object creation expression new Array(…) with the
>same arguments.

## `String`について

>15.5.1 The String Constructor Called as a Function
>When String is called as a function rather than as a constructor, it performs a type conversion.
>15.5.1.1 String ( [ value ] )
>Returns a String value (not a String object) computed by ToString(value). If value is not supplied, the empty
>String “” is returned.

## `Boolean`について

>15.6.1 The Boolean Constructor Called as a Function
>When Boolean is called as a function rather than as a constructor, it performs a type conversion.
>15.6.1.1 Boolean (value)
>Returns a Boolean value (not a Boolean object) computed by ToBoolean(value).

## `Number`について

>15.7.1 The Number Constructor Called as a Function
>When Number is called as a function rather than as a constructor, it performs a type conversion.
>15.7.1.1 Number ( [ value ] )
>Returns a Number value (not a Number object) computed by ToNumber(value) if value was supplied, else
>returns +0.

## `RegExp`について

>15.10.3.1 RegExp(pattern, flags)
>If pattern is an object R whose [[Class]] internal property is “RegExp” and flags is undefined, then return R
>unchanged. Otherwise call the standard built-in RegExp constructor (15.10.4.1) as if by the expression new
>RegExp(pattern, flags) and return the object constructed by that constructor.

## `Error`について

>15.11.1 The Error Constructor Called as a Function
>When Error is called as a function rather than as a constructor, it creates and initialises a new Error object.
>Thus the function call Error(…) is equivalent to the object creation expression new Error(…) with the
>same arguments.

# 参考リンク

* [ECMAScript Language Specification 5.1 Edition](http://www.ecma-international.org/ecma-262/5.1/)