静态 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.

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

句法

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

参数

target

The target function to call.

argumentsList
An array-like object specifying the arguments with which target should be called.
newTarget 可选
The constructor whose prototype should be used. See also the new.target operator. If newTarget is not present, its value defaults to target .

返回值

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

异常

A TypeError , if target or newTarget are not constructors.

描述

Reflect.construct() allows you to invoke a constructor with a variable number of arguments. (This would also be possible by using the spread syntax combined with the new operator .)

let obj = new Foo(...args)
let 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:
let obj1 = Reflect.construct(OneClass, args, OtherClass)
// ...has the same result as this:
let 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
//Another example to demonstrate below:
function func1(a, b, c, d) {
  console.log(arguments[3]);
}
function func2(d, e, f, g) {
    consol.log(arguments[3]);
}
let obj1 = Reflect.construct(func1, ['I', 'Love', 'my', 'India'])
obj1
					

However, while the end result is the same, there is one important difference in the process. When using Object.create() and Function.prototype.apply() 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)
}
let obj1 = Reflect.construct(OneClass, args)
// Output:
//     OneClass
//     function OneClass { ... }
let obj2 = Reflect.construct(OneClass, args, OtherClass)
// Output:
//     OneClass
//     function OtherClass { ... }
let obj3 = Object.create(OtherClass.prototype);
OneClass.apply(obj3, args)
// Output:
//     OneClass
//     undefined
					

范例

使用 Reflect.construct()

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

规范

规范
ECMAScript (ECMA-262)
The definition of 'Reflect.construct' in that specification.

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
construct Chrome 49 Edge 12 Firefox 42 IE No Opera 36 Safari 10 WebView Android 49 Chrome Android 49 Firefox Android 42 Opera Android 36 Safari iOS 10 Samsung Internet Android 5.0 nodejs 6.0.0

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改: