1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
---
title: Reflect.construct()
slug: Web/JavaScript/Reference/Global_Objects/Reflect/construct
tags:
- ECMAScript 2015
- JavaScript
- Method
- Reference
- Reflect
- Polyfill
browser-compat: javascript.builtins.Reflect.construct
translation_of: Web/JavaScript/Reference/Global_Objects/Reflect/construct
---
{{JSRef}}
静的な **`Reflect.construct()`** メソッドは {{jsxref("Operators/new", "new")}} 演算子のように、ただし関数として動作します。これは `new target(...args)` の呼び出しと同等です。このメソッドはオプションを追加することで、別なプロトタイプを指定することができます。
{{EmbedInteractiveExample("pages/js/reflect-construct.html", "taller")}}
## 構文
```js
Reflect.construct(target, argumentsList)
Reflect.construct(target, argumentsList, newTarget)
```
### 引数
- `target`
- : 呼び出し対象の関数。
- `argumentsList`
- : 配列風オブジェクトで、 `target` の呼び出しの引数を指定する。
- `newTarget` {{optional_inline}}
- : プロトタイプを使用するコンストラクター。 [`new.target`](/ja/docs/Web/JavaScript/Reference/Operators/new.target) も参照してください。 `newTarget` が存在しない場合は、既定値は `target` になります。
### 返値
`target` (または、もしあれば `newTarget`) の新しいインスタンスで、 `target` に `argumentsList` を渡してコンストラクターとして呼び出すことで初期化します。
### 例外
{{jsxref("TypeError")}}: `target` または `newTarget` がコンストラクターではない場合。
## 解説
`Reflect.construct()` によって、可変長引数を指定してコンストラクターを呼び出すことができます。 (これは[スプレッド構文](/ja/docs/Web/JavaScript/Reference/Operators/Spread_syntax)と[`new` 演算子](/ja/docs/Web/JavaScript/Reference/Operators/new)を組み合わせて使用することでも可能です。)
```js
let obj = new Foo(...args)
let obj = Reflect.construct(Foo, args)
```
### `Reflect.construct()` と `Object.create()`
`Reflect` が導入される前は、オブジェクトを構築するのにコンストラクターとプロトタイプの任意の組み合わせで {{jsxref("Object.create()")}} を使用して構築することができました。
function OneClass() {
this.name = 'one'
}
function OtherClass() {
this.name = 'other'
}
// Calling this:
let obj1 = Reflect.construct(OneClass, args, OtherClass)
// ...has the same result as this:
let obj2 = Object.create(OtherClass.prototype)
OneClass.apply(obj2, args)
console.log(obj1.name) // 'one'
console.log(obj2.name) // 'one'
console.log(obj1 instanceof OneClass) // false
console.log(obj2 instanceof OneClass) // false
console.log(obj1 instanceof OtherClass) // true
console.log(obj2 instanceof OtherClass) // true
//Another example to demonstrate below:
function func1(a, b, c, d) {
console.log(arguments[3]);
}
function func2(d, e, f, g) {
console.log(arguments[3]);
}
let obj1 = Reflect.construct(func1, ['I', 'Love', 'my', 'India'])
obj1
この 2 つの手法の最終結果は同じですが、その過程に重要な違いがあります。 `Object.create()` と {{jsxref("Function.prototype.apply()")}} を使用する場合、 `new.target` 演算子はコンストラクター内で `undefined` を返します。これは、 `new` 演算子を用いないためです。
一方、 `Reflect.construct()` を呼び出す場合は、 `new.target` 演算子は、提供されていれば `newTarget` を指し、そうでなければ `target` を指します。
```js
function OneClass() {
console.log('OneClass')
console.log(new.target)
}
function OtherClass() {
console.log('OtherClass')
console.log(new.target)
}
let obj1 = Reflect.construct(OneClass, args)
// Output:
// OneClass
// function OneClass { ... }
let obj2 = Reflect.construct(OneClass, args, OtherClass)
// Output:
// OneClass
// function OtherClass { ... }
let obj3 = Object.create(OtherClass.prototype);
OneClass.apply(obj3, args)
// Output:
// OneClass
// undefined
```
## 例
### `Reflect.construct()` の使用
```js
let d = Reflect.construct(Date, [1776, 6, 4])
d instanceof Date // true
d.getFullYear() // 1776
```
## 仕様書
{{Specifications}}
## ブラウザーの互換性
{{Compat}}
## 関連情報
- `Reflect.construct` のポリフィルが [`core-js`](https://github.com/zloirock/core-js#ecmascript-reflect) にあります
- {{jsxref("Reflect")}}
- {{jsxref("Operators/new", "new")}}
- [`new.target`](/ja/docs/Web/JavaScript/Reference/Operators/new.target)
|