W3cubDocs

/JavaScript

Reflect.construct

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.
newTarget Optional
The constructor to be used. See also the new.target operator. If newTarget is not present, it is target.

Return value

A new instance of the given target, created by calling it (or newTarget, if present) as a constructor with the given arguments.

Exceptions

A 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 for 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

Browser compatibility

Feature Chrome Edge Firefox Internet Explorer Opera Safari
Basic support 49 12 42 No 36 10
Feature Android webview Chrome for Android Edge mobile Firefox for Android Opera Android iOS Safari Samsung Internet
Basic support 49 49 Yes 42 36 10 ?

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect/construct