Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Reflect.construct — invoke a constructor with an argument list

Reflect.construct invokes a constructor with arguments read from an object. Its optional newTarget separates the constructor being executed from the new.target value and the usual prototype selection.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

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

Reflect.construct(target, argumentsList) performs construction, not an ordinary function call. It also works with classes. Unlike Reflect.apply, it does not accept an existing this.

argumentsList must be an object. Reflect.construct reads its length to determine which indexed properties to read. A missing length is treated as 0, so if target is a constructor, Reflect.construct(target, {}) invokes it with no arguments. In contrast, the spread syntax in new target(...argumentsList) takes arguments from the iterator. An array with a custom iterator can therefore pass different arguments to the same constructor in the two calls. Being iterable alone is insufficient; a Set does not supply its elements to Reflect.construct. Both target and a supplied newTarget must be constructors — arrow functions are not. Invalid arguments cause TypeError; exceptions from the constructor propagate.

Omitting newTarget defaults it to target; explicitly passing undefined causes TypeError. Reflect.construct passes newTarget to target's internal [[Construct]] operation. If target is an ordinary constructor function, new.target in its body is newTarget. If target is a Proxy, however, its construct trap can ignore newTarget and invoke the underlying constructor with a different new.target. During ordinary object creation, the object's prototype comes from newTarget.prototype when that value is an object. This does not execute the body of newTarget. If target explicitly returns an object, that object can replace the created instance.

In the example, Person sets the fields, but the prototype comes from Alternate. The result is therefore not an instance of Person. This is an ECMAScript language facility, not a Web API; availability in particular browsers requires separate checking.

Examples

function Person(name) {
  this.name = name;
  this.usedAlternate = new.target === Alternate;
}
function Alternate() {
  throw new Error("Must not run");
}

const person = Reflect.construct(Person, ["Ada"], Alternate);
console.log(person.name);
console.log(person.usedAlternate);
console.log(Object.getPrototypeOf(person) === Alternate.prototype);
console.log(person instanceof Person);

Result

Ada
true
true
false

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources