Javascript "Not constructor" Exception while creating objects

I define the object as follows:

function Project(Attributes, ProjectWidth, ProjectHeight) { this.ProjectHeight = ProjectHeight; this.ProjectWidth = ProjectWidth; this.ProjectScale = this.GetProjectScale(); this.Attributes = Attributes; this.currentLayout = ''; this.CreateLayoutArray = function() {....} } 

Then I try to create an instance, for example:

 var newProj = new Project(a,b,c); 

But this is an exception:

 Project is not a constructor 

What could be wrong? I walked a lot, but still can not understand what I'm doing wrong.

+70
javascript object constructor typeerror
Apr 11 2018-12-12T00:
source share
12 answers

The code posted in the question cannot generate this error because Project not a user-defined function / valid constructor.

 function x(a,b,c){} new x(1,2,3); // produces no errors 

You probably did something like this:

 function Project(a,b,c) {} Project = {}; // or possibly Project = new Project new Project(1,2,3); // -> TypeError: Project is not a constructor 

Variable declarations using var are raised and thus always evaluated to the rest of the code. Thus, it can also cause problems:

 function Project(){} function localTest() { new Project(1,2,3); // 'Project' points to the local variable, // not the global constructor! //...some noise, causing you to forget that the 'Project' constructor was used var Project = 1; // Evaluated first } 
+71
Apr 11 2018-12-12T00:
source share

An additional reason for this may be the function of the arrow ES2015. They cannot be used as constructors .

 const f = () => {}; new f(); // This throws "f is not a constructor" 
+36
May 04 '16 at 20:40
source share

I also found googled and found this solution:

You have a Project variable somewhere that is not a function. Then the new operator will complain about it. Try console.log(Project) where you would use it as construcotr and you will find it.

+19
Apr 11 2018-12-12T00:
source share

For me, these were the differences between import and require on ES6.

eg.

 // processor.js class Processor { } export default Processor //index.js const Processor = require('./processor'); const processor = new Processor() //fails with the error import Processor from './processor' const processor = new Processor() // succeeds 
+17
Dec 23 '16 at 11:32
source share

In my case, I used the name of the prototype as the name of the object. For example,

 function proto1() {} var proto1 = new proto1(); 

It was a stupid mistake, but it might help someone like me;)

+9
Jul 31 '16 at 14:55
source share

For my project, the problem turned out to be a circular link created by require () calls:

 y.js: var x = require("./x.js"); var y = function() { console.log("result is " + x(); } module.exports = y; x.js: var y = require("./y.js"); var my_y = new y(); // <- TypeError: y is not a constructor var x = function() { console.log("result is " + my_y; } module.exports = x; 

The reason is that when he tries to initialize y, he creates a temporary object "y" (not a class, an object!) In the dependency system, which somehow is not yet a constructor. Then, when x.js is finished, it can continue creating the y constructor. Only in x.js is there an error in it where it tries to use the non-constructor of y.

+8
Oct 18 '16 at 17:43
source share

To add @wprl to the answer, the transcript of the ES6 object method, like the arrow functions, cannot be used as a constructor. πŸ˜…

 const o = { a: () => {}, b() {}, c: function () {} }; const { a, b, c } = o; new a(); // throws "a is not a constructor" new b(); // throws "b is not a constructor" new c(); // works 
+2
Jul 15 '17 at 15:07
source share

In my case, I forgot the open and closed brackets at the end of the function definition that wraps all my code in the exported module. That is, I had:

 (function () { 'use strict'; module.exports.MyClass = class{ ... ); 

Instead:

 (function () { 'use strict'; module.exports.MyClass = class{ ... )(); 

The compiler does not complain, but the require statement in the import module does not set the variable to which it is assigned, so it is undefined at the point you are trying to build it, and it will give TypeError: MyClass is not a constructor error.

+1
Mar 03 '17 at 2:24 on
source share

I had a similar error, and my problem was that the name and case of the variable name and constructor name were identical, which does not work, because javascript interprets the proposed constructor as a new created variable.

In other words:

 function project(name){ this.name = name; } //elsewhere... //this is no good! name/case are identical so javascript barfs. let project = new project('My Project'); 

Just changing the name or variable name fixes the problem:

 //with a capital 'P' function Project(name){ this.name = name; } //elsewhere... //works! class name/case is dissimilar to variable name let project = new Project('My Project'); 
+1
Sep 22 '17 at 20:29
source share

This is because you used another variable called "project" in your code. Something like var project = {}

To make the code work, modify it as follows:

var project = {} in var project1 = {}

+1
Dec 26 '17 at 17:19
source share

I just want to add that if the constructor is called from another file, then something is as simple as forgetting to export the constructor with

 module.exports = NAME_OF_CONSTRUCTOR 

will also throw a non constructor exception.

0
Oct 27 '18 at 8:36
source share

Car.js

 class Car { getName() {return 'car'}; } export default Car; 

TestFile.js

 const object = require('./Car.js'); const instance = new object(); 

error: TypeError: instance is not a constructor

print object contents

 object = {default: Car} 

add require function by default and it will work as contructor

 const object = require('object-fit-images').default; const instance = new object(); instance.getName(); 
-one
Jun 26 '18 at 15:03
source share



All Articles