Reflect.construct vs new(此处的对象类型)

时间:2016-10-01 18:44:14

标签: javascript typescript

使用ES6的Reflect.construct后,我注意到将对象类型参数化为new关键字也有效。

https://jsfiddle.net/mnw349o5/

function Nice() {
    return Date;
}


window.alert(new (Nice())); // will show the date today

window.alert(Reflect.construct(Nice(), [])); // same as above

window.alert(new Nice()); // not the intended

我是否还应该使用将对象类型作为参数传递给new关键字的方法来创建动态对象,因为它似乎也有效?不使用Reflect.construct将是项目的一个较少的节点模块依赖项。

目前我正在使用和谐反射到polyfill Reflect,因此我可以在ES5定位的TypeScript项目中使用它。

1 个答案:

答案 0 :(得分:1)

在您需要Reflect之前没有理由使用construct(在 public class ReleaseVersion { public string Version { get; set; } public DateTime? ReleaseDate { get; set; } public IEnumerable<ReleaseNote> ReleaseNotes { get; set; } } public class ReleaseNote { public string Description { get; set; } public string Progress { get; set; } public string Status { get; set; } } 的情况下,用于将newtarget设置为任意值)。你不需要它,所以你不应该使用它。