Movatterモバイル変換


[0]ホーム

URL:


  1. Tecnologia Web para desenvolvedores
  2. JavaScript
  3. Referência JavaScript
  4. Objetos Globais
  5. Reflect
  6. Reflect.construct()

Esta página foi traduzida do inglês pela comunidade.Saiba mais e junte-se à comunidade MDN Web Docs.

View in EnglishAlways switch to English

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.

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: 6

Sintaxe

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

Parametros

target

A função alvo à ser chamada.

argumentsList

Um objeto tipo array que especifica com quais argumentostarget deveria ser chamada.

newTargetOptional

O construtor de quem o protótipo deveria ser usado. Veja também onew.target operador. SenewTarget nã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).

js
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().

js
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); // 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.

When invokingReflect.construct(), on the other hand, thenew.target operator will point to thenewTarget parameter if supplied, ortarget if not.

js
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//     undefined

Examples

UsingReflect.construct()

js
var d = Reflect.construct(Date, [1776, 6, 4]);d instanceof Date; // trued.getFullYear(); // 1776

Especificações

Specification
ECMAScript® 2026 Language Specification
# sec-reflect.construct

Compatibilidade com navegadores

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp