Javascript “Not a Constructor” Exception while creating objects

后端 未结 14 1243
[愿得一人]
[愿得一人] 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: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.

提交回复
热议问题