Categories
JavaScript Answers

How to fix JavaScript “Not a Constructor” Exception while creating objects?

Spread the love

Sometimes, we want to fix JavaScript "Not a Constructor" Exception while creating objects.

In this article, we’ll look at how to fix JavaScript "Not a Constructor" Exception while creating objects.

How to fix JavaScript "Not a Constructor" Exception while creating objects?

To fix JavaScript "Not a Constructor" Exception while creating objects, we should create constructors with regular functions or the class syntax.

For instance, instead of writing

const F = () => {};
const f = new F();

We write

class F {}
const f = new F();

to replace the arrow function with class F.

class is syntactic sugar for constructor functions.

Conclusion

To fix JavaScript "Not a Constructor" Exception while creating objects, we should create constructors with regular functions or the class syntax.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *