gpt4 book ai didi

javascript - Reflect.construct vs new(此处为对象类型)

转载 作者:搜寻专家 更新时间:2023-10-30 21:12:41 25 4
gpt4 key购买 nike

在使用了 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 将减少项目的节点模块依赖性。

目前我正在使用 harmony-reflect 来填充 Reflect,这样我就可以在面向 ES5 的 TypeScript 项目中使用它。

最佳答案

没有理由使用 Reflect 直到您需要它(在 construct 的情况下,将用于将 newtarget 设置为任意值)。你不需要它,所以你不应该使用它。

关于javascript - Reflect.construct vs new(此处为对象类型),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39810256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com