Javascript “Not a Constructor” Exception while creating objects

后端 未结 14 1137
[愿得一人]
[愿得一人] 2020-11-27 18:15

I am defining an object like this:

function Project(Attributes, ProjectWidth, ProjectHeight)
{
    this.ProjectHeight = ProjectHeight;
    this.ProjectWidth          


        
相关标签:
14条回答
  • 2020-11-27 18:45

    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

    printing content of object

    object = {default: Car}
    

    append default to the require function and it will work as contructor

    const object = require('object-fit-images').default;
    const instance = new object();
    instance.getName();
    
    0 讨论(0)
  • 2020-11-27 18:46

    It is happening because you must have used another variable named "project" in your code. Something like var project = {}

    For you to make the code work, change as follows:

    var project = {} into var project1 = {}

    0 讨论(0)
  • 2020-11-27 18:47

    Sometimes it is just how you export and import it. For this error message it could be, that the default keyword is missing.

    export default SampleClass {}
    

    Where you instantiate it:

    import SampleClass from 'path/to/class';
    let sampleClass = new SampleClass();
    

    Option 2, with curly braces:

    export SampleClass {}
    import { SampleClass } from 'path/to/class';
    let sampleClass = new SampleClass();
    
    0 讨论(0)
  • 2020-11-27 18:48

    For my project, the problem turned out to be a circular reference created by the 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 it is attempting to initialize y, it creates a temporary "y" object (not class, object!) in the dependency system that is somehow not yet a constructor. Then, when x.js is finished being defined, it can continue making y a constructor. Only, x.js has an error in it where it tries to use the non-constructor y.

    0 讨论(0)
  • 2020-11-27 18:56

    In my case I was using the prototype name as the object name. For e.g.

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

    It was a silly mistake but might be of help to someone like me ;)

    0 讨论(0)
  • 2020-11-27 18:57

    In my case I'd forgotten the open and close parantheses at the end of the definition of the function wrapping all of my code in the exported module. I.e. I had:

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

    Instead of:

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

    The compiler doesn't complain, but the require statement in the importing module doesn't set the variable it's being assigned to, so it's undefined at the point you try to construct it and it will give the TypeError: MyClass is not a constructor error.

    0 讨论(0)
提交回复
热议问题