Reflect.construct() 方法的行为有点像 new 操作符 构造函数 , 相当于运行 new target(...args).

语法

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

参数

target
被运行的目标函数
argumentsList
调用构造函数的数组或者伪数组
newTarget 可选
该参数为构造函数, 参考 new.target 操作符,如果没有newTarget参数, 默认和target一样

Errors thrown

抛出TypeError,异常, 如果target或者newTarget不是构造函数

描述

Reflect.construct允许你使用可变的参数来调用构造函数 

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

实例

使用 Reflect.construct()

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

使用 newTarget 参数

参考 classes 获取更多有关子类的信息,  和new.target 操作符的信息

function someConstructor() {}
var result = Reflect.construct(Array, [], someConstructor);

Reflect.getPrototypeOf(result); // 输出:someConstructor.prototype
Array.isArray(result); // true

规范

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
Reflect.construct
Standard Initial definition.
ECMAScript Latest Draft (ECMA-262)
Reflect.construct
Draft  

浏览器兼容

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 49 42 (42) 未实现 未实现 未实现
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 未实现 未实现 42.0 (42) 未实现 未实现 未实现

相关链接