Home Web Front-end HTML Tutorial What is a front-end modular ESM?

What is a front-end modular ESM?

Feb 25, 2024 am 11:48 AM
front end Key words esm

What is a front-end modular ESM?

What is front-end ESM, specific code examples are required

In front-end development, ESM refers to ECMAScript Modules, which is a modular development method based on ECMAScript specifications. ESM brings many benefits, such as better code organization, isolation between modules, and reusability. This article will introduce the basic concepts and usage of ESM and provide some specific code examples.

  1. Basic concepts of ESM
    In ESM, we can divide the code into multiple modules, and each module exposes some interfaces for use by other modules. Each module can introduce the dependencies it needs through the import statement without worrying about global variable conflicts. At the same time, modules can also expose their interfaces to other modules through the export statement.
  2. Usage of ESM
    2.1 Basic syntax
    To use ESM, you need to use the script tag in the HTML file to load the module and specify the type as "module". For example:
<script type="module" src="main.js"></script>
Copy after login

In the module file, we can use the import statement to introduce the interfaces of other modules, and use the export statement to expose our own interfaces to other modules. For example, we have two module files:

// module1.js
export function sayHello() {
  console.log("Hello, module1!");
}

// module2.js
import { sayHello } from "./module1.js";

sayHello();
Copy after login

2.2 Export and Import Interface
In ESM, you can use the export statement to export a variable, function or class in the module to other modules. For example:

// module1.js
export const PI = 3.14;

export function square(x) {
  return x * x;
}
Copy after login

Other modules can use the import statement to import the interface in module1.js and use it. For example:

// module2.js
import { PI, square } from "./module1.js";

console.log(PI); // 输出3.14
console.log(square(2)); // 输出4
Copy after login

2.3 Default export and default import
In addition to exporting named interfaces, ESM also supports default export and default import. A module can only have one default export, and the default export does not need to be wrapped with {}. The default import can use any variable name to receive. For example:

// module1.js
export default function sayGoodbye() {
  console.log("Goodbye!");
}

// module2.js
import goodbye from "./module1.js";

goodbye(); // 输出Goodbye!
Copy after login
  1. The difference between ESM and CommonJS (module.exports/require)
    ESM and CommonJS are two different modular development methods. ESM uses static import and export, and module dependencies are determined at compile time, while CommonJS uses dynamic import and export, and module dependencies can only be determined at runtime.

The benefit of ESM is that the dependencies of modules are clearer and there is no need to use global variables to control the loading and execution order of modules. The advantage of CommonJS is that it can dynamically calculate module dependencies at runtime, giving it greater flexibility.

The following is an example of mixing ESM and CommonJS:

// module1.js (ESM)
export const PI = 3.14;

// module2.js (CommonJS)
const { PI } = require("./module1.js");
console.log(PI); // 输出3.14
Copy after login

Summary:
ESM is a commonly used modular development method in front-end development. It manages modules through static import and export. reference relationship. In ESM, modules can reference each other and reuse code, and there is no need to worry about the pollution of global variables. In actual development, we can split complex codes according to modular ideas to improve the maintainability and readability of the code.

The above is an introduction to the basic concepts and usage of ESM. I hope that through the introduction of this article, readers can have a certain understanding of ESM and be able to apply ESM technology in actual development.

The above is the detailed content of What is a front-end modular ESM?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to adjust aperture on Xiaomi Mi 14 Ultra? How to adjust aperture on Xiaomi Mi 14 Ultra? Mar 19, 2024 am 09:01 AM

How to adjust aperture on Xiaomi Mi 14 Ultra?

How to set Chinese in Cheat Engine? How to set Chinese in ce modifier How to set Chinese in Cheat Engine? How to set Chinese in ce modifier Mar 18, 2024 pm 01:20 PM

How to set Chinese in Cheat Engine? How to set Chinese in ce modifier

DaVinci Resolve Studio now supports AV1 hardware encoding for AMD graphics cards DaVinci Resolve Studio now supports AV1 hardware encoding for AMD graphics cards Mar 06, 2024 pm 10:04 PM

DaVinci Resolve Studio now supports AV1 hardware encoding for AMD graphics cards

How to update Honor MagicOS 8.0 on Honor 90 GT? How to update Honor MagicOS 8.0 on Honor 90 GT? Mar 18, 2024 pm 06:46 PM

How to update Honor MagicOS 8.0 on Honor 90 GT?

Planet Mojo: Building a Web3 game metaverse from the auto-chess game Mojo Melee Planet Mojo: Building a Web3 game metaverse from the auto-chess game Mojo Melee Mar 14, 2024 pm 05:55 PM

Planet Mojo: Building a Web3 game metaverse from the auto-chess game Mojo Melee

PHP and Vue: a perfect pairing of front-end development tools PHP and Vue: a perfect pairing of front-end development tools Mar 16, 2024 pm 12:09 PM

PHP and Vue: a perfect pairing of front-end development tools

Questions frequently asked by front-end interviewers Questions frequently asked by front-end interviewers Mar 19, 2024 pm 02:24 PM

Questions frequently asked by front-end interviewers

Simplify file upload processing with Golang functions Simplify file upload processing with Golang functions May 02, 2024 pm 06:45 PM

Simplify file upload processing with Golang functions

See all articles