79218729

Date: 2024-11-23 20:09:33
Score: 0.5
Natty:
Report link

No, this is not how modules are used. You are trying to use CommonJS syntax with its require and module.exports. With classes, you rather need ES6 syntax, that is, defined by ECMAScript standards starting v.6. Please see this Stackoverflow answer. If by some reason you have to use CommonJS way (one case where it is still required is the Visual Studio Code extension API), you can restructure your code to do so, we can discuss it. Let's see how to do it with ES6.

In one module, you can define and export a class, for example,

export class Movie { /* ... */ }

Alternatively, you can export several objects in one line:

export { Movie, AnotherClass, SomeFunction, someObject };

To use the class Movie outside this module, you need to import it

import { Movie } from "relative path to your module file name";
const movieInstance = new Movie(/* ... */);
// ...
movieInstance.displayMovieDetails();

Note that you can selectively choose what to import. You can also use alias names for your imported objects.

Please see the MDN Guide JavaScript modules.

Reasons:
  • Blacklisted phrase (1): Stackoverflow
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Sergey A Kryukov