0

I have an ES6 class file that looks like so:

import { BaseClass } from './xm/classes/class-file.js';

export class myClass extends BaseClass {
   constructor() {
      super();
   }

   createFormSession(formId, payload) {
     return new NewForm(formId, payload);
   }
}

class NewForm {
  constructor(formId, payload) {
    this.formId = formId;
    this.payload = payload;
  }
}

I am getting this error on that says expecting myClass is not a constructor.

So it seems like it's expecting BaseClass but it can't find it and I am unclear as to why, when it was working in my local development server.

The actual line of code that causes the error is where the first constructor() function is referenced and the error is appearing in console of Chrome browser.

The way it's being called:

var defaultUIHandler = new window.myClass()

I have also tried:

var defaultUIHandler = new window.myClass()
halfer
  • 19,824
  • 17
  • 99
  • 186
Daniel
  • 14,004
  • 16
  • 96
  • 156
  • Two questions. First, can you show the actual line of code that causes the error? And, second, are you attempting to run this in the browser (which seems implied by the reference to `window.myClass`)? There is no actual invocation of the `myClass` constructor in the code you show - we need to see the code that causes the error. – jfriend00 Jul 27 '20 at 20:53
  • @jfriend00, added that info, thank you. – Daniel Jul 27 '20 at 20:55
  • 1
    Please show us the code that calls your myClass constructor - probably `let someObj = new myClass()`. That's probably where your error starts from. That's what I want to see. – jfriend00 Jul 27 '20 at 20:56
  • Are you 100% sure that `import { BaseClass } from './xm/classes/class-file.js';` is working properly when you change environments. If it works in one environment, but not another, that seems like the most likely difference. – jfriend00 Jul 27 '20 at 20:58

1 Answers1

0

ES6 class names are not automatically added to the global object like other top level variables are in the browser. This is part of the ES6 specification. So you cannot access them via the window object unless you specifically assign them as properties of the window object.

So, this code:

var defaultUIHandler = new window.myClass()

will not properly find myClass as it is not, by default, a property of the window object. If you've either defined myClass in this scope or imported it into this scope, then you should be able to just do:

var defaultUIHandler = new myClass();
jfriend00
  • 683,504
  • 96
  • 985
  • 979