Skip to content
Tutorials Center
My learning

Topics

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.

JavaScript
function Thing() {
  if (!new.target) {
    throw new TypeError("Call Thing with new");
  }
}

Try it

JS Detecting a call with new

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Abstract classes and subclasses

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
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.target is inherited from the enclosing function, like this.
  • Abstract base class pattern. if (new.target === Base) throw ... prevents direct instantiation but allows subclasses.
  • Subclass-aware initialization. new.target.prototype is the prototype the new object should have, which is what Object.create(new.target.prototype) would use.
  • Not the same as this.constructor. this.constructor can be overwritten or inherited from somewhere unexpected; new.target is set by the engine for that specific call.
  • Classes cannot be called without new, so they throw automatically. new.target checks 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 of new.target yourself.
  • Getters and methods called normally always see undefined.

Browser support

Supported in every current browser (ES2015).