LoginSignup
7
7

More than 5 years have passed since last update.

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

Last updated at Posted at 2013-09-18

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

ES5での仕様

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

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

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

以下ECMAScript 5の仕様書より抜粋します。

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.

参考リンク

7
7
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7