New.target with operator prefix

"The new.target property allows you to determine whether a function or constructor has been called using the new operator" [1]

I can use new.target in the if statement to new.target error if the function was not called with new :

 if(!new.target){ throw new Error('Must be called with new keyword!') } 

However, Safari prevents the use of new.target with ! so with the error message

new.target cannot appear after prefix operator

I traced this to this line in Webkit .

However, a positive condition can be checked!

 if(new.target){} else{ throw new Error('Must be called with new keyword!') } 

Is this a bug with the syntax engine? Or, alternatively, should new.target be used in how they are applied?

simple playback: https://codepen.io/mdjasper/pen/eEWORY?editors=0012


Edit: this issue was filed on webkit bugzilla: https://bugs.webkit.org/show_bug.cgi?id=157970

+5
source share
1 answer

This syntax must be supported, and the error was acknowledged as a webkit error:

 if(!new.target){ throw new Error('Must be called with new keyword!') } 

The patch was written and merged and will ship with a future version of webkit

https://bugs.webkit.org/show_bug.cgi?id=157970#c17

Until the fix gets into the released version, the workaround is to explicitly check new.target

 if(new.target === undefined){ throw new Error('Must be called with new keyword!') } 
0
source

Source: https://habr.com/ru/post/1270699/


All Articles