new.target with a prefix operator

Viewed 338

"The new.target property lets you detect whether a function or constructor was called using the new operator" [1]

I can use new.target in an if statement to throw an error if a function was not called using new:

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

However, safari prevents new.target from being used with the ! in this way, with the error message

new.target can't come after a prefix operator

I tracked this down to this line in Webkit.

However the positive condition can be checked!

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

Is this an error with safari's parsing engine? Or alternatively, should I be using new.target in the way they enforce?

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


Edit: This issue has been filed on webkit bugzilla: https://bugs.webkit.org/show_bug.cgi?id=157970

1 Answers
Related