Michael Buen
Michael Buen

Reputation: 39443

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.

Upvotes: 3

Views: 776

Answers (1)

Bergi
Bergi

Reputation: 665286

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.

Upvotes: 1

Related Questions