箭头函数可以用 new 吗?
参考答案
不能。new (() => {})() 抛 TypeError: xxx is not a constructor。
原因:
- 箭头函数没有
[[Construct]]内部方法; - 没有
prototype属性((()=>{}).prototype === undefined); this由词法决定,无法绑定到新实例;- 没有
new.target、arguments、super。
如需构造能力用普通函数或 class。
箭头函数可以用 new 吗?
不能。new (() => {})() 抛 TypeError: xxx is not a constructor。
原因:
[[Construct]] 内部方法;prototype 属性((()=>{}).prototype === undefined);this 由词法决定,无法绑定到新实例;new.target、arguments、super。如需构造能力用普通函数或 class。
本题来自 计算机知识面试题库,共 921 道题。