TypeError: is not a constructor

TypeError: is not a constructor

The “TypeError: is not a constructor” is a common JavaScript error that occurs when you try to use the new keyword to create an instance of an object that is not a constructor function.

A constructor function is a special type of function in JavaScript that is used to create new objects. The syntax for creating a new object using a constructor function is new ConstructorName(arguments).

Here’s an example of a constructor function:

function Person(name) {
  this.name = name;
}

let person = new Person("John");
console.log(person.name); // Output: "John"

In the above example, Person is a constructor function, and new Person("John") creates a new object with the name property set to “John”.

However, if you try to use the new keyword with a non-constructor function or object, you will get the “TypeError: is not a constructor” error, for example:

let person = new Object(); // or let person = {}
console.log(person.name); // Output: "TypeError: Object is not a constructor"

This error can also occur if you accidentally use the wrong variable name or if you’re trying to create an instance of a class that is not yet defined. In that case, you should check your variable and class names to make sure they are spelled correctly.

To fix this issue, you can either make sure that the object is a constructor function, or you can create the object without the new keyword:

let person = {};
console.log(person);

or

let person = Object.create({});
console.log(person);

It is also worth to keep in mind that, javascript built-in objects

Leave a Reply