🔸new.target
in normal function calls, new.target is undefined
.
function User(name) {
// ⭐️ if called normally, add `new`.
if (!new.target) { return new User(name); }
this.name = name;
}
let joe = User('Joe'); // ⭐️ `new` not required now
in class constructors (or function called using new), new.target refers to the constructor/function that was directly invoked by new. for example:
new A(); // new.target === A
new B(); // new.target === B
⚠️ class constructors cannot be invoked without 'new'.
new.target "pseudo-property" is available in all functions.
Last updated
Was this helpful?