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
149
150
151
152
153
154
155
156
157
|
---
title: Reflect.construct()
slug: Web/JavaScript/Reference/Global_Objects/Reflect/construct
translation_of: Web/JavaScript/Reference/Global_Objects/Reflect/construct
---
<div>{{JSRef}}</div>
<div>Die statische Methode <code><strong>Reflect</strong></code><strong><code>.construct()</code></strong> verhält sich wie der <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new"><code>new</code> operator</a>, allerdings in Form einer Funktion.</div>
<div>Sie ist das Equivalent zu <code>new target(...args)</code>. Es existiert zudem die Option, einen anderen Prototyp zu definieren.</div>
<div> </div>
<div>{{EmbedInteractiveExample("pages/js/reflect-construct.html")}}</div>
<h2 id="Syntax">Syntax</h2>
<pre class="syntaxbox">Reflect.construct(target, listeDerArgumente[, newTarget])
</pre>
<h3 id="Parameter">Parameter</h3>
<dl>
<dt>target</dt>
<dd>Die Funktion, die aufgerufen werden soll.</dd>
<dt>listeDerArgumente</dt>
<dd>Ein Array ähnliches Objekt welches die Parameter spezifiziert, mit denen die Zielfunktion aufgerufen wird.</dd>
<dt><code>newTarget</code> {{optional_inline}}</dt>
<dd>Der Construktor dessen Typ genutzt werden soll. Siehe auch den <code><a href="/en-US/docs/Web/JavaScript/Reference/Operators/new.target">new.target</a></code> Operator. Wenn <code>newTarget</code> nicht existiert, ist es <code>target</code>.</dd>
</dl>
<h3 id="Rückgabewert">Rückgabewert</h3>
<p>Eine neue Instanz des <code>target</code> (oder <code>newTarget</code>, wenn vorhanden), Initialisiert durch das <code>target</code> als Konstruktor mit den gegebenen Parametern.</p>
<h3 id="Ausnahmen">Ausnahmen</h3>
<p>Es wird ein {{jsxref("TypeError")}} geworfen, Wenn <code>target</code> or <code>newTarget</code> keine Konstruktoren sind.</p>
<h2 id="Beschreibung">Beschreibung</h2>
<p><code>Reflect.construct</code> erlaubt es dir, einen Konstruktor mit einer variablen Anzahl von Parametern aufzurufen. (Was auch möglich ist, bai Verwendung des <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator">spread operator</a> in Kombination mit dem <a href="/en-US/docs/Web/JavaScript/Reference/Operators/new">new operator</a>).</p>
<pre class="brush: js">var obj = new Foo(...args);
var obj = Reflect.construct(Foo, args);
</pre>
<p> </p>
<h3 id="Reflect.construct()_vs_Object.create()"><code>Reflect.construct()</code> vs <code>Object.create()</code></h3>
<p>Befor wir mit der Einführung von <code>Reflect</code> beginnen. Objekte können durch von <code>Object.create()</code> mithilfe einer beliebigen Kombination aus Konstruktor und Prototyp erstellt werden.</p>
<pre class="brush: js">function OneClass() {
this.name = 'one';
}
function OtherClass() {
this.name = 'other';
}
// Calling this:
var obj1 = Reflect.construct(OneClass, args, OtherClass);
// ...has the same result as this:
var 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
</pre>
<p>Obwohl das Endergebnis das gleiche ist, gibt es einen wichtigen Unterschied im Prozess.</p>
<p>Wenn <code>Object.create()</code> und <code>Function.prototype.apply()</code> verwendet werden, zeigt der <code>new.target</code> Operator innerhalb der als Konstruktor verwendeten Funktion auf <code>undefined</code>, da das <code>newTarget</code> nicht zum Erstellen des Objekts verwendet wird.</p>
<p>Wenn Sie dagegen <code>Reflect.construct()</code> aufrufen, zeigt der <code>new.target</code> Operator auf den <code>newTarget</code> Parameter, sofern angegeben und auf <code>target</code>, falls nicht.</p>
<pre class="brush: js">function OneClass() {
console.log('OneClass');
console.log(new.target);
}
function OtherClass() {
console.log('OtherClass');
console.log(new.target);
}
var obj1 = Reflect.construct(OneClass, args);
// Output:
// OneClass
// function OneClass { ... }
var obj2 = Reflect.construct(OneClass, args, OtherClass);
// Output:
// OneClass
// function OtherClass { ... }
var obj3 = Object.create(OtherClass.prototype);
OneClass.apply(obj2, args);
// Output:
// OneClass
// undefined</pre>
<p> </p>
<h2 id="Beispiele">Beispiele</h2>
<h3 id="Verwendung_Reflect.construct()">Verwendung <code>Reflect.construct()</code></h3>
<pre class="brush: js">var d = Reflect.construct(Date, [1776, 6, 4]);
d instanceof Date; // true
d.getFullYear(); // 1776
</pre>
<h2 id="Spezifikationen">Spezifikationen</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Spezifikation</th>
<th scope="col">Status</th>
<th scope="col">Kommentar</th>
</tr>
<tr>
<td>{{SpecName('ES2015', '#sec-reflect.construct', 'Reflect.construct')}}</td>
<td>{{Spec2('ES2015')}}</td>
<td>Ursprüngliche Definition.</td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-reflect.construct', 'Reflect.construct')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="Browser_kompabilität">Browser kompabilität</h2>
<p>{{Compat("javascript.builtins.Reflect.construct")}}</p>
<h2 id="See_also">See also</h2>
<ul>
<li>{{jsxref("Reflect")}}</li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new"><code>new</code></a></li>
<li><code><a href="/en-US/docs/Web/JavaScript/Reference/Operators/new.target">new.target</a></code></li>
</ul>
|