Importing and using classes from a JavaScript module

One of the benefits of using modules is the ability to import and use classes from other modules. This helps break down a large codebase into smaller, more manageable pieces. Let’s take a look at how to import and use classes from a JavaScript module.

Assuming we have a module called myModule.js with the following code:

export class MyClass {
  constructor(name) {
    this.name = name;
  }

  sayHello() {
    console.log(`Hello, ${this.name}!`);
  }
}

To use the MyClass from another JavaScript file, we need to import it using the import statement. Here’s an example:

import { MyClass } from './myModule.js';

const myInstance = new MyClass('John');
myInstance.sayHello(); // Output: Hello, John!

In the above code snippet, we use the import keyword followed by curly braces {} to specify which classes or functions we want to import from the module. We provide the relative path to the module file ./myModule.js.

Once imported, we can create an instance of the MyClass using the new keyword and call its methods as usual.

It is important to note that for the import statement to work, the module containing the class or function must be exported using the export keyword. In our example, we exported the MyClass using export class MyClass { ... }, making it accessible for importing in other modules.

In conclusion, importing and using classes from JavaScript modules is a powerful and convenient way to organize and reuse code. By breaking down code into smaller modules, we can maintain better code quality and improve development efficiency.

#JavaScript #Modules