ECMAScript 6 new.target
function Foo() {
if (!new.target) throw "Foo() must be called with new";
}
Foo(); // throws "Foo() must be called with new"
new Foo();
class A {
constructor() {
console.log(new.target.name);
}
}
class B extends A {
constructor() {
super();
}
}
new A(); // A
new B(); // B
function fn () {
const arrFn = () => {
console.log(new.target.name)
}
return arrFn();
}
new fn(); // fn
fn(); // TypeError: Cannot read property 'name' of undefined
// target is undefined最后更新于