Esta página foi traduzida do inglês pela comunidade.Saiba mais e junte-se à comunidade MDN Web Docs.
Reflect.construct()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since setembro de 2016.
The staticReflect.construct() method acts like thenew operator, but as a function. It is equivalent to callingnew target(...args). It gives also the added option to specify a different prototype.
In this article
Experimente
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: 6Sintaxe
Reflect.construct(target, argumentsList[, newTarget])
Parametros
targetA função alvo à ser chamada.
argumentsListUm objeto tipo array que especifica com quais argumentos
targetdeveria ser chamada.newTargetOptionalO construtor de quem o protótipo deveria ser usado. Veja também o
new.targetoperador. SenewTargetnão estiver presente, serátarget.
Return value
A new instance oftarget (ornewTarget, if present), initialized bytarget as a constructor with the given arguments.
Exceptions
ATypeError, iftarget ornewTarget 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 thespread operator combined with thenew operator).
var obj = new Foo(...args);var obj = Reflect.construct(Foo, args);Reflect.construct() vsObject.create()
Prior to the introduction ofReflect, objects could be constructed using an arbitrary combination of constructor and prototype by usingObject.create().
function OneClass() { this.name = "one";}function OtherClass() { this.name = "other";}// Calling this:var obj1 = Reflect.construct(OneClass, args, OtherClass);// ...has the same result as this:var 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); // trueHowever, 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.
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);}var obj1 = Reflect.construct(OneClass, args);// Output:// OneClass// function OneClass { ... }var obj2 = Reflect.construct(OneClass, args, OtherClass);// Output:// OneClass// function OtherClass { ... }var obj3 = Object.create(OtherClass.prototype);OneClass.apply(obj3, args);// Output:// OneClass// undefinedExamples
>UsingReflect.construct()
var d = Reflect.construct(Date, [1776, 6, 4]);d instanceof Date; // trued.getFullYear(); // 1776Especificações
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-reflect.construct> |