JavaScript is new.target Metaproperty

ES6 offers a metaproperty called new.target to identify whether a function or constructor runs using the new operator. The target attribute, dot, and new keyword build the object of the new.target. All methods have access to the new.target metaproperty.

In contrast, the new.target in arrow functions is the one that pertains to the related function. Use the new.target property to check at execution whether a function is being performed as a function or as a constructor. The new operator can be used from within a parent class to identify a particular derived class that was invoked.

Syntax

The following syntax shows a new JavaScript target property.

It is the simple function used with the script tag.

Examples

The multiple examples show how to work new.target property in different ways.

Example 1:

The following example shows a basic JavaScript new target property. You can get the message of the given function using the property.

Output

The following image shows the function's information.

JavaScript is new.target Metaproperty

Example 2:

The following example shows a basic JavaScript new target property. You can get the message of the given function with the target name of the web page.

Output

The following image shows the target of the function information.

JavaScript is new.target Metaproperty

Example 3:

The following example shows a basic JavaScript new target property. You can get the message of the given function with a new operator using the property. Here we use class in the script tag of the web application with the constructor functionality.

Output

The following image shows the function's information.

JavaScript is new.target Metaproperty

Example 4:

The following example shows a basic JavaScript new target property. You can get the message of the given function without a new operator using the property. Here, we use the constructor th get the required output data using the onclick function.

Output

The following image shows the function's information.

JavaScript is new.target Metaproperty

Example 5:

The following example shows a basic JavaScript new target property. You can get the message of the given function with the constructor using the property. You can extend the function and call the new target property a constructor.

Output

The following image shows the function's information.

JavaScript is new.target Metaproperty

Conclusion

The new javascript target property gets the constructor and methods available in the function. It helps to know the available methods used in the web application.