Reflect.construct()
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2016.
TheReflect.construct()
static method is like thenew
operator, but as a function. It is equivalent to callingnew target(...args)
. It additionally allows to specify a differentnew.target
value.
Try it
function func1(a, b, c) { this.sum = a + b + c;}const args = [1, 2, 3];const object1 = new func1(...args);const object2 = Reflect.construct(func1, args);console.log(object2.sum);// Expected output: 6console.log(object1.sum);// Expected output: 6
Syntax
Reflect.construct(target, argumentsList)Reflect.construct(target, argumentsList, newTarget)
Parameters
target
The target function to call.
argumentsList
Anarray-like object specifying the arguments with which
target
should be called.newTarget
OptionalThe value of the
new.target
expression insidetarget
. Defaults totarget
. Generally (see example),target
specifies thelogic to initialize the object, whilenewTarget.prototype
specifies theprototype of the constructed object.
Return value
A new instance oftarget
(ornewTarget
, if present), initialized bytarget
as a constructor with the givenargumentsList
.
Exceptions
TypeError
Thrown if
target
ornewTarget
is not a constructor, or ifargumentsList
is not an object.
Description
Reflect.construct()
provides the reflective semantic of a constructor call. That is,Reflect.construct(target, argumentsList, newTarget)
is semantically equivalent to:
new target(...argumentsList);
Note that when using thenew
operator,target
andnewTarget
are always the same constructor — butReflect.construct()
allows you to pass a differentnew.target
value. Conceptually,newTarget
is the function on whichnew
was called, andnewTarget.prototype
will become the constructed object's prototype, whiletarget
is the constructor that is actually executed to initialize the object. For example,new.target
may also be different from the currently executed constructor in class inheritance.
class A { constructor() { console.log(new.target.name); }}class B extends A {}new B(); // "B"
Reflect.construct()
allows you to invoke a constructor with a variable number of arguments. (This is also possible with thespread syntax in a normal constructor call.)
const obj = new Foo(...args);const obj = Reflect.construct(Foo, args);
Reflect.construct()
invokes the[[Construct]]
object internal method oftarget
.
Examples
Using Reflect.construct()
const d = Reflect.construct(Date, [1776, 6, 4]);d instanceof Date; // trued.getFullYear(); // 1776
Changing new.target
IfnewTarget
is passed, it changes the value ofnew.target
inside the constructor. The constructed object will be an instance ofnewTarget
, nottarget
.
function OneClass() { console.log("OneClass executed"); console.log(`new.target is ${new.target.name}`);}function OtherClass() { console.log("OtherClass executed"); console.log(`new.target is ${new.target.name}`);}const obj1 = Reflect.construct(OneClass, []);// Logs:// OneClass executed// new.target is OneClassconsole.log(obj1 instanceof OneClass); // trueconst obj2 = Reflect.construct(OneClass, [], OtherClass);// Logs:// OneClass executed// new.target is OtherClassconsole.log(obj2 instanceof OtherClass); // trueconsole.log(obj2 instanceof OneClass); // false
Of course, there's no strong guarantee about the prototype chain of the constructed object, as it depends on the constructor's implementation. For example, if thetarget
constructor returns an object, then that object will be the constructed object, regardless of thenewTarget
value. Iftarget
is a proxy with aconstruct
trap, then the trap fully controls the construction process.
function OneClass() { return { name: "one" };}function OtherClass() { return { name: "other" };}const obj1 = Reflect.construct(OneClass, [], OtherClass);console.log(obj1.name); // 'one'console.log(obj1 instanceof OneClass); // falseconsole.log(obj1 instanceof OtherClass); // false
A validnew.target
should be a constructor function with aprototype
property, but the latter is not enforced. If theprototype
property's value is not an object, the initialized object will inherit fromObject.prototype
.
function OneClass() { console.log("OneClass executed"); console.log(`new.target is ${new.target.name}`);}function OtherClass() { console.log("OtherClass executed"); console.log(`new.target is ${new.target.name}`);}OtherClass.prototype = null;const obj = Reflect.construct(OneClass, [], OtherClass);// Logs:// OneClass executed// new.target is OtherClassconsole.log(Object.getPrototypeOf(obj) === Object.prototype); // true
Reflect.construct() vs. Object.create()
Prior to the introduction ofReflect
, objects could be constructed using an arbitrary combination of constructors and prototypes usingObject.create()
.
function OneClass() { this.name = "one";}function OtherClass() { this.name = "other";}const args = [];const obj1 = Reflect.construct(OneClass, args, OtherClass);const obj2 = Object.create(OtherClass.prototype);OneClass.apply(obj2, args);console.log(obj1.name); // 'one'console.log(obj2.name); // 'one'console.log(obj1 instanceof OneClass); // falseconsole.log(obj2 instanceof OneClass); // falseconsole.log(obj1 instanceof OtherClass); // trueconsole.log(obj2 instanceof OtherClass); // true
However, while the end result is the same, there is one important difference in the process. When usingObject.create()
andFunction.prototype.apply()
, thenew.target
operator will point toundefined
within the function used as the constructor, since thenew
keyword is not being used to create the object. (In fact, it uses theapply
semantic, notconstruct
, although normal functions happen to operate nearly the same.)
When invokingReflect.construct()
, on the other hand, thenew.target
operator will point to thenewTarget
parameter if supplied, ortarget
if not.
function OneClass() { console.log("OneClass"); console.log(new.target);}function OtherClass() { console.log("OtherClass"); console.log(new.target);}const obj1 = Reflect.construct(OneClass, args);// Logs:// OneClass// function OneClass { ... }const obj2 = Reflect.construct(OneClass, args, OtherClass);// Logs:// OneClass// function OtherClass { ... }const obj3 = Object.create(OtherClass.prototype);OneClass.apply(obj3, args);// Output:// OneClass// undefined
Specifications
Specification |
---|
ECMAScript® 2026 Language Specification # sec-reflect.construct |