0

I want to create subclasses in different js files and extend a superclass in a different js file. So I'm exporting the superclass, and then importing the superclass in all the other js files. But I'm getting an error. What am I doing wrong?

superclassfile.js:

export { Process };

class Process {
    constructor() {
    }
}


subclassfile.js

import { Process } from 'superclassfile.js';

class SubProcess extends Process {
.....
}


Error: Uncaught ReferenceError: Cannot access 'Process' before initialization

Daniel Gros
  • 43
  • 1
  • 5
  • 1
    @CertainPerformance No. The location of the `export` declaration doesn't matter. While `class` initialisation is not hoisted, `export` declarations are. If you get a temporal dead zone problem with an exported variable, the issue is usually a circular dependency. – Bergi Oct 08 '19 at 00:37
  • The code, as posted, is fine. To clarify the "circular dependency" point above, is it possible that your `superclassfile.js` file tries to import something that then loads `subclassfile.js`? If so, it could be trying to extend `Process` before `class Process {}` has actually evaluated (since it is still running the imports for that file). – loganfsmyth Oct 08 '19 at 00:41
  • I think it's a temporal dead zone, just initialize it before access. – polunzh Oct 08 '19 at 00:50

0 Answers0