The static Reflect
.construct()
method acts like the new
operator as a function. It is equivalent to calling new target(...args)
.
Reflect.construct(target, argumentsList[, newTarget])
target
argumentsList
target
should be called.newTarget
Optional
new.target
operator. If newTarget
is not present, it is target
.A new instance of the given target, created by calling it (or newTarget
, if present) as a constructor with the given arguments.
A TypeError
, if target
or newTarget
are not constructors.
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);
Reflect.construct()
var d = Reflect.construct(Date, [1776, 6, 4]); d instanceof Date; // true d.getFullYear(); // 1776
newTarget
parameterSee 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
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Reflect.construct' in that specification. | Standard | Initial definition. |
ECMAScript 2017 Draft (ECMA-262) The definition of 'Reflect.construct' in that specification. | Draft |
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 49 | 12 | 42 (42) | No support | 36 | 10 |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | No support | 49 | 42.0 (42) | No support | No support | 10 |
© 2005–2017 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