Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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

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.

like image 921
Michael Buen Avatar asked May 15 '26 04:05

Michael Buen


1 Answers

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.

like image 76
Bergi Avatar answered May 16 '26 17:05

Bergi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!