Our volunteers haven't translated this article into Bamanankan yet. Join us and help get the job done!
You can also read the article in English (US).
The new.target
property lets you detect whether a function or constructor was called using the new operator. In constructors and functions instantiated with the new operator, new.target
returns a reference to the constructor or function. In normal function calls, new.target
is undefined
.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Syntax
new.target
Description
The new.target
syntax consists of the keyword "new
", a dot, and a property name "target"
. Normally "new."
serves as the context for a property access, but here "new."
is not really an object. In constructor calls, however, new.target
refers to the constructor invoked by new
and so "new.
" becomes a virtual context.
The new.target
property is a meta property that is available to all functions. In arrow functions, new.target
refers to the new.target
of the surrounding function.
Examples
new.target in function calls
In normal function calls (as opposed to constructor function calls), new.target
is undefined
. This lets you detect if a function was called with new as a constructor.
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 in constructors
In class constructors, new.target
refers to the constructor that was directly invoked by new
. This is also the case if the constructor is in a parent class and was delegated from a child constructor.
class A { constructor() { console.log(new.target.name); } } class B extends A { constructor() { super(); } } var a = new A(); // logs "A" var b = new B(); // logs "B" class C { constructor() { console.log(new.target); } } class D extends C { constructor() { super(); } } var c = new C(); // logs class C{constructor(){console.log(new.target);}} var d = new D(); // logs class D extends C{constructor(){super();}}
Thus from the above example of class C and D, it seems that new.target
points to the class Definition of class which is initialized. For example, when D was initialized using new, the class definition of D was printed and similarly in case of c, class C was printed
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Built-in Function Objects' in that specification. |
Standard | Initial definition. |
ECMAScript Latest Draft (ECMA-262) The definition of 'Built-in Function Objects' in that specification. |
Draft |
Browser compatibility
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Basic support | Chrome Full support 46 | Edge Full support Yes | Firefox Full support 41 | IE No support No | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 46 | Chrome Android Full support 46 | Edge Mobile Full support Yes | Firefox Android Full support 41 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 5.0 | nodejs Full support 5.0.0 |
Legend
- Full support
- Full support
- No support
- No support