new.target 带前缀运算符
new.target with a prefix operator
"The new.target property lets you detect whether a function or constructor was called using the new operator" [1]
如果未使用 new
:
调用函数,我可以在 if 语句中使用 new.target
来抛出错误
if(!new.target){
throw new Error('Must be called with new keyword!')
}
但是,safari 会阻止 new.target
以这种方式与 !
一起使用,并显示错误消息
new.target can't come after a prefix operator
我追踪到 this line in Webkit。
但是可以检查阳性条件!
if(new.target){}
else{
throw new Error('Must be called with new keyword!')
}
这是 safari 解析引擎的错误吗?或者,我应该按照他们执行的方式使用new.target
吗?
简单重现:https://codepen.io/mdjasper/pen/eEWORY?editors=0012
编辑:此问题已在 webkit bugzilla 上归档:https://bugs.webkit.org/show_bug.cgi?id=157970
应该支持这种语法,确认错误是webkit bug:
if(!new.target){
throw new Error('Must be called with new keyword!')
}
已编写并合并补丁,并将随 webkit 的未来版本一起发布
https://bugs.webkit.org/show_bug.cgi?id=157970#c17
在发布版本中修复之前,解决方法是显式检查 new.target
if(new.target === undefined){
throw new Error('Must be called with new keyword!')
}
"The new.target property lets you detect whether a function or constructor was called using the new operator" [1]
如果未使用 new
:
new.target
来抛出错误
if(!new.target){
throw new Error('Must be called with new keyword!')
}
但是,safari 会阻止 new.target
以这种方式与 !
一起使用,并显示错误消息
new.target can't come after a prefix operator
我追踪到 this line in Webkit。
但是可以检查阳性条件!
if(new.target){}
else{
throw new Error('Must be called with new keyword!')
}
这是 safari 解析引擎的错误吗?或者,我应该按照他们执行的方式使用new.target
吗?
简单重现:https://codepen.io/mdjasper/pen/eEWORY?editors=0012
编辑:此问题已在 webkit bugzilla 上归档:https://bugs.webkit.org/show_bug.cgi?id=157970
应该支持这种语法,确认错误是webkit bug:
if(!new.target){
throw new Error('Must be called with new keyword!')
}
已编写并合并补丁,并将随 webkit 的未来版本一起发布
https://bugs.webkit.org/show_bug.cgi?id=157970#c17
在发布版本中修复之前,解决方法是显式检查 new.target
if(new.target === undefined){
throw new Error('Must be called with new keyword!')
}