DEV Community

Rahul Sharma
Rahul Sharma

Posted on • Updated on


Exploring the Power of in JavaScript

In JavaScript, the property is a meta-property that can be used to determine whether a function was called with the new keyword. It returns a reference to the constructor function that was invoked with the new keyword, or undefined if the function was not called with new.

function MyClass() {
  if ( {
    console.log('MyClass was called with new');
  } else {
    console.log('MyClass was not called with new');

new MyClass(); // MyClass was called with new

MyClass(); // MyClass was not called with new
Enter fullscreen mode Exit fullscreen mode can be useful for implementing custom constructor functions, where you want to ensure that the function is always called with new, or for creating abstract base classes that cannot be instantiated directly.

function MyClass() {
  if (! {
    throw new Error('MyClass must be called with new');
Enter fullscreen mode Exit fullscreen mode
If you enjoyed reading this short blog, you may also want to check out my javascript short playlist on YouTube.

Must Read If you haven't
3 steps to create custom state management library with React and Context API
How to cancel Javascript API request with AbortController
Getting started with SolidJs – A Beginner's Guide

More content at
Catch me on YouTube, Github, Twitter, LinkedIn, Medium, Stackblitz, Hashnode, HackerNoon, and Blogspot.

Top comments (0)

Visualizing Promises and Async/Await 🤯

async await

Learn the ins and outs of Promises and Async/Await!