{{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. IfnewTarget
is not present, it istarget
.
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()")}}