JavaScript/new.target
来自康健生活
引言
new
使用构造函数时,使用 new 操作符进行调用,观察 this 指向。
使用 new
this 指向的是新创建的对象 模板:Leftrightflex
这里添加 return 没有意义。
不使用 new
this 遵循普通函数的 this 定义(比如在浏览器中指向 window 对象)。
使用 return 。
修复
new.target
new 调用,则函数内 new.target 指向构造函数;否则其值为 undefined。
bind
普通函数 bind 操作,可以返回一个绑定的固定 this 的新函数。
构造函数则不同。bind 操作不能改变构造函数内的 this。
No categories