Наши волонтёры ещё не перевели данную статью на Русский. Присоединяйтесь к нам и помогите сделать эту работу!
Вы можете также прочитать эту статью на English (US).

The static Reflect.construct() method acts like the new operator, but as a function. It is equivalent to calling new target(...args). It gives also the added option to specify a different prototype.

Syntax

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

Parameters

target
The target function to call.
argumentsList
An array-like object specifying the arguments with which target should be called.
newTarget Optional
The constructor whose prototype should be used. See also the new.target operator. If newTarget is not present, it is target.

Return value

A new instance of target (or newTarget, if present), initialized by target as a constructor with the given arguments.

Exceptions

A TypeError, if target or newTarget 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 the spread operator combined with the new operator).

var obj = new Foo(...args);
var obj = Reflect.construct(Foo, args);

 

Reflect.construct() vs Object.create()

Prior to the introduction of Reflect, objects could be constructed using an arbitrary combination of constructor and prototype by using Object.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); // false
console.log(obj2 instanceof OneClass); // false

console.log(obj1 instanceof OtherClass); // true
console.log(obj2 instanceof OtherClass); // true

However, while the end result is the same, there is one important difference in the process. When using Object.create() and Function.prototype.apply(), the new.target operator will point to undefined within the function used as the constructor, since the new keyword is not being used to create the object.

When invoking Reflect.construct(), on the other hand, the new.target operator will point to the newTarget parameter if supplied, or target 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(obj2, args);
// Output:
//     OneClass
//     undefined

 

Examples

Using Reflect.construct()

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

Specifications

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Reflect.construct' in that specification.
Standard Initial definition.
ECMAScript Latest Draft (ECMA-262)
The definition of 'Reflect.construct' in that specification.
Draft  

Browser compatibility

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung InternetNode.js
Basic supportChrome Full support 49Edge Full support 12Firefox Full support 42IE No support NoOpera Full support 36Safari Full support 10WebView Android Full support 49Chrome Android Full support 49Edge Mobile Full support YesFirefox Android Full support 42Opera Android Full support 36Safari iOS Full support 10Samsung Internet Android Full support 5.0nodejs Full support 6.0.0

Legend

Full support  
Full support
No support  
No support

See also

Метки документа и участники

Внесли вклад в эту страницу: pascallothar, safrankamate, wbamberg, fscholz, jameshkramer, kdex, David_Gilbertson, eduardoboucas, gingerik, Waldo, evilpie
Обновлялась последний раз: pascallothar,