The new.target
meta-property lets you detect whether a function or constructor was called using the new
operator. In constructors and functions invoked using the new
operator, new.target
returns a reference to the constructor or function that new
was called upon. In normal function calls, new.target
is undefined
.
function Foo() {
if (!new.target) {
throw new TypeError('calling Foo constructor without new is invalid');
}
}
try {
Foo();
} catch (e) {
console.log(e);
// Expected output: TypeError: calling Foo constructor without new is invalid
}
标签:function,target,whether,Javascript,constructor,new,Foo From: https://www.cnblogs.com/Answer1215/p/18451616