Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 893207 of Reflect.construct()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Reflect/construct
  • Revision title: Reflect.construct()
  • Revision id: 893207
  • Created:
  • Creator: evilpie
  • Is current revision? No
  • Comment Function#call and Function#apply only call a function and don't allow constructing behavior

Revision Content

{{JSRef}} {{harmony}}

The static Reflect.construct() method acts like the new operator as a function. It is equivalent to calling new target(...args).

Syntax

Reflect.construct(target, argumentsList[, newTarget])

Parameters

target
The target function to call.
argumentsList
An array-like object, specifying the arguments with which target should be called, or {{jsxref("null")}} or {{jsxref("undefined")}} if no arguments should be provided to the function.
newTarget {{optional_inline}}
The constructor to be used. See also the new.target operator. If newTarget is not present, it is target.

Errors thrown

A {{jsxref("TypeError")}}, if target or newTarget are not constructors.

Description

Reflect.construct allows you to invoke a constructor with a variable number of arguments (which would also be possible by using the spread operator combined with the new operator).

var obj = new Foo(...args);
var obj = Reflect.construct(Foo, args);

Examples

Using Reflect.construct()

var d = Reflect.construct(Date, [1776, 6, 4]);
d instanceof Date; // true
d.getFullYear(); // 1776

Using the newTarget parameter

See also Classes more information about sub classing and the new.target operator.

function someConstructor() {}
var result = Reflect.construct(Array, [], someConstructor);

Reflect.getPrototypeOf(result); // someConstructor.prototype
Array.isArray(result); // true

Specifications

Specification Status Comment
{{SpecName('ES6', '#sec-reflect.construct', 'Reflect.construct')}} {{Spec2('ES6')}} Initial definition.

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatNo}} {{CompatGeckoDesktop(42)}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatNo}} {{CompatNo}} {{CompatGeckoMobile(42)}} {{CompatNo}} {{CompatNo}} {{CompatNo}}

See also

  • {{jsxref("Reflect")}}
  • {{jsxref("Function.prototype.apply()")}}

Revision Source

<div>{{JSRef}} {{harmony}}</div>

<p>The static <code><strong>Reflect</strong></code><strong><code>.construct()</code></strong> method acts like the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new"><code>new</code> operator</a> as a function. It is equivalent to calling <code>new target(...args)</code>.</p>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">
Reflect.construct(target, argumentsList[, newTarget])
</pre>

<h3 id="Parameters">Parameters</h3>

<dl>
 <dt><code>target</code></dt>
 <dd>The target function to call.</dd>
 <dt><code>argumentsList</code></dt>
 <dd>An array-like object, specifying the arguments with which <code>target</code> should be called, or {{jsxref("null")}} or {{jsxref("undefined")}} if no arguments should be provided to the function.</dd>
 <dt><code>newTarget</code> {{optional_inline}}</dt>
 <dd>The constructor to be used. See also the <code><a href="/en-US/docs/Web/JavaScript/Reference/Operators/new.target">new.target</a></code> operator. If <code>newTarget</code> is not present, it is <code>target</code>.</dd>
</dl>

<h3 id="Errors_thrown">Errors thrown</h3>

<p>A {{jsxref("TypeError")}}, if <code>target</code> or <code>newTarget</code> are not constructors.</p>

<h2 id="Description">Description</h2>

<p><code>Reflect.construct</code> allows you to invoke a constructor with a variable number of arguments (which would also be possible by using the <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator">spread operator</a> combined with the <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>

<h2 id="Examples">Examples</h2>

<h3 id="Using_Reflect.construct()">Using <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>

<h3 id="Using_the_newTarget_parameter">Using the <code>newTarget</code> parameter</h3>

<p>See also <a href="/en-US/docs/Web/JavaScript/Reference/Classes">Classes</a> more information about sub classing and the <code><a href="/en-US/docs/Web/JavaScript/Reference/Operators/new.target">new.target</a></code> operator.</p>

<pre class="brush: js">
function someConstructor() {}
var result = Reflect.construct(Array, [], someConstructor);

Reflect.getPrototypeOf(result); // someConstructor.prototype
Array.isArray(result); // true
</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-reflect.construct', 'Reflect.construct')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoDesktop(42)}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoMobile(42)}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{jsxref("Reflect")}}</li>
 <li>{{jsxref("Function.prototype.apply()")}}</li>
</ul>
Revert to this revision