Michael Buen Michael Buen - 2 years ago 140
TypeScript Question

Reflect.construct vs new ( object type here )

After using ES6's Reflect.construct, I noticed that parameterizing the object type to new keyword also works.


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

Should I still use the approach of passing the object type as a parameter on new keyword to create dynamic object since it seems to also work? Not using Reflect.construct would be one less node module dependency for the project.

Currently I'm using harmony-reflect to polyfill Reflect so I can use it on ES5-targeting TypeScript project.

Answer Source

There's no reason to use Reflect until you need it (which, in the case of construct, would be for setting the newtarget to arbitrary values). You don't need it, so you shouldn't use it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download