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:

1

<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:

1

2

3

4

5

6

7

8

9

// 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:

1

2

3

4

5

6

// 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:

1

2

3

4

5

// 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:

1

2

3

4

5

6

7

8

9

// 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:

1

2

3

4

5

6

// 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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
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

Adjusting the aperture size has a crucial impact on the photo effect. Xiaomi Mi 14 Ultra provides unprecedented flexibility in camera aperture adjustment. In order to allow everyone to adjust the aperture smoothly and realize the free adjustment of the aperture size, the editor here brings you a detailed tutorial on how to set the aperture on Xiaomi Mi 14Ultra. How to adjust the aperture on Xiaomi Mi 14Ultra? Start the camera, switch to &quot;Professional Mode&quot;, and select the main camera - W lens. Click on the aperture, open the aperture dial, A is automatic, select f/1.9 or f/4.0 as needed.

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

Ce Modifier (CheatEngine) is a game modification tool dedicated to modifying and editing game memory. So how to set Chinese in CheatEngine? Next, the editor will tell you how to set Chinese in Ce Modifier. I hope it can Help friends in need. In the new software we download, it can be confusing to find that the interface is not in Chinese. Even though this software was not developed in China, there are ways to convert it to the Chinese version. This problem can be solved by simply applying the Chinese patch. After downloading and installing the CheatEngine (ce modifier) ​​software, open the installation location and find the folder named languages, as shown in the figure below

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

Honor 90GT is a cost-effective smartphone with excellent performance and excellent user experience. However, sometimes we may encounter some problems, such as how to update Honor MagicOS8.0 on Honor 90GT? This step may be different for different mobile phones and different models. So, let us discuss how to upgrade the system correctly. How to update Honor MagicOS 8.0 on Honor 90GT? According to news on February 28, Honor today pushed the MagicOS8.0 public beta update for its three mobile phones 90GT/100/100Pro. The package version number is 8.0.0.106 (C00E106R3P1) 1. Ensure your Honor The battery of the 90GT is fully charged;

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

Popular Metaverse game projects founded in the last crypto cycle are accelerating their expansion. On March 4, PlanetMojo, the Web3 game metaverse platform, announced a number of important developments in its game ecology, including the announcement of the upcoming parkour game GoGoMojo, the launch of the new season "Way of War" in the flagship auto-chess game MojoMelee, and the celebration of the new The first ETH series "WarBannerNFT" launched this season in cooperation with MagicEden. In addition, PlanetMojo also revealed that they plan to launch Android and iOS mobile versions of MojoMelee later this year. This project will be launched at the end of 2021. After nearly two years of hard work in the bear market, it will soon be completed.

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

Recent news, lackMagic has launched the 18.5PublicBeta2 public beta update of the DaVinci Resolve Studio video editing software, bringing AV1 encoding support to AMD Radeon graphics cards. After updating to the latest version, AMD graphics card users will be able to take advantage of hardware acceleration for AV1 encoding in DaVinci Resolve Studio. Although the official does not specify the supported architectures or models, it is expected that all AMD graphics card users can try this feature. In 2018, AOMedia released a new video coding standard AV1 (AOMediaVideoCodec1.0). AV1 is produced by a number of

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. In today's era of rapid development of the Internet, front-end development has become increasingly important. As users have higher and higher requirements for the experience of websites and applications, front-end developers need to use more efficient and flexible tools to create responsive and interactive interfaces. As two important technologies in the field of front-end development, PHP and Vue.js can be regarded as perfect tools when paired together. This article will explore the combination of PHP and Vue, as well as detailed code examples to help readers better understand and apply these two

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

Answer: Yes, Golang provides functions that simplify file upload processing. Details: The MultipartFile type provides access to file metadata and content. The FormFile function gets a specific file from the form request. The ParseForm and ParseMultipartForm functions are used to parse form data and multipart form data. Using these functions simplifies the file processing process and allows developers to focus on business logic.

How to install dual SIM on Realme 12 Pro? How to install dual SIM on Realme 12 Pro? Mar 18, 2024 pm 02:10 PM

Although the general operations of domestic mobile phones are very similar, there are still some differences in some details. For example, different mobile phone models and manufacturers may have different dual-SIM installation methods. Erzhenwo 12Pro, a new mobile phone, also supports dual-SIM dual standby, but how should dual-SIM be installed on this phone? How to install dual SIM on Realme 12Pro? Remember to turn off your phone before installation. Step 1: Find the SIM card tray: Find the SIM card tray of the phone. Usually, in the Realme 12 Pro, the SIM card tray is located on the side or top of the phone. Step 2: Insert the first SIM card. Use a dedicated SIM card pin or a small object to insert it into the slot in the SIM card tray. Then, carefully insert the first SIM card.

See all articles