new.target

new.target属性允许你检测函数或构造方法是否是通过new运算符被调用的。在通过new运算符被初始化的函数或构造方法中,new.target返回一个指向构造方法或函数的引用。在普通的函数调用中,new.target 的值是undefined

function Foo() {
  if (!new.target) throw "Foo() must be called with new";
  console.log("Foo instantiated with new");
}

Foo(); // throws "Foo() must be called with new"
new Foo(); // logs "Foo instantiated with new"

new.target可以帮助我们判断一个函数或者类是否是通过new运算符被调用的

posted @ 2021-07-01 16:23  Sebastian·S·Pan  阅读(47)  评论(0编辑  收藏  举报