JavaScript Reference ยท Operators
The new.target meta property
Reference for new.target in JavaScript: detecting whether a function was called with new, abstract base classes, inheritance behavior and arrow functions.
new.target is a meta property available inside functions and class constructors. When the function was invoked with new, it refers to the constructor that new was applied to; in a normal call it is undefined. It lets a function behave differently depending on how it was called, and it lets a base class know which subclass is actually being created.
function Thing() { if (!new.target) { throw new TypeError("Call Thing with new"); } }
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
new.target
Return value: the constructor function or class that new (or Reflect.construct()) was invoked on, or undefined if the function was called normally. In a derived class, a super() call passes the original new.target up to the parent, so the parent sees the subclass.
Exceptions: new.target is a SyntaxError outside of a function. At the top level of a script or module it is not allowed.
Usage notes
- Inside arrow functions
new.targetis inherited from the enclosing function, likethis. - Abstract base class pattern.
if (new.target === Base) throw ...prevents direct instantiation but allows subclasses. - Subclass-aware initialization.
new.target.prototypeis the prototype the new object should have, which is whatObject.create(new.target.prototype)would use. - Not the same as
this.constructor.this.constructorcan be overwritten or inherited from somewhere unexpected;new.targetis set by the engine for that specific call. - Classes cannot be called without
new, so they throw automatically.new.targetchecks matter mostly in old-style constructor functions or when you want a friendlier fix-up behavior. Reflect.construct(target, args, newTarget)lets you choose the value ofnew.targetyourself.- Getters and methods called normally always see
undefined.
Browser support
Supported in every current browser (ES2015).