search
HomeWeb Front-endFront-end Q&AWhat is es6 modularity

What is es6 modularity

Apr 19, 2022 pm 01:41 PM
es6

es6 modularization is a modular development specification common to browsers and servers. Its design idea is to be as static as possible, so that module dependencies and input and output variables can be determined during compilation. In ES6 modularization, each js file is an independent module. The import keyword is used to import the module and the expost keyword is used to export it.

What is es6 modularity

The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.

Classification of front-end modular specifications

Before the birth of the ES6 modular specification, the JavaScript community had already tried and proposed modular specifications such as AMD, CMD, and CommonJS.

However, these modular standards proposed by the community still have certain differences and limitations, and are not universal modular standards for browsers and servers, for example:

  • AMD and CMD are suitable for browser-side Javascript modularization

  • CommonJS is suitable for server-side Javascript modularization

What is es6 modularity

ES6 modularity is a modular development specification common to both the browser and the server.

Its emergence has greatly reduced the modular learning cost of front-end developers. Developers do not need to learn additional modular specifications such as AMD, CMD or CommonJS

ES6 module The design idea is to be as static as possible, so that the module dependencies, as well as input and output variables, can be determined at compile time.

Definition in the ES6 modular specification:

  • Each js file is an independent module

  • Use the import keyword to import other module members

  • Use the expost keyword to share module members externally

Usage:

① Default export and default import

② On-demand export and on-demand import

③ Directly import and execute the code in the module

Default export and default import

Default export syntax:

export default default exported members

Default import syntax:

import receives name from 'module identifier'

let n1 = 10 //定义模块私有成员n1
let n2 = 20 //定义模块私有成员n2 (外界访问不到n2 因为他没有共享出去)

function show() {} //定义模块私有方法 show

export default { //使用export default 默认导出语法 向外共享n1 和 show 两个成员
    n1,show
}

Note Points:

① In each module, only one export default is allowed, otherwise an error will be reported

② The receiving name during default import can be any name, as long as it is legal The member name is

On-demand import and on-demand export

On-demand import syntax:

export type members

Export on demand syntax:

import {member} from 'module identifier'

import aixos from '@/utils/request.js'

// login 请求
export const userLogin = (data) => {
    return aixos({
        method: 'post',
        url: '/login',
        data
    })
}

// register 请求
export const userRegister = (data) => {
    return aixos({
        method: 'post',
        url: '/register',
        data
    })
}

Note:

① Multiple on-demand exports can be used in each module

② The member names imported on-demand must be consistent with the names exported on-demand

③ When importing on demand, you can use the as keyword to rename

④ Importing on demand can be used together with the default import

The above is the detailed content of What is es6 modularity. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
The Size of React's Ecosystem: Navigating a Complex LandscapeThe Size of React's Ecosystem: Navigating a Complex LandscapeApr 28, 2025 am 12:21 AM

TonavigateReact'scomplexecosystemeffectively,understandthetoolsandlibraries,recognizetheirstrengthsandweaknesses,andintegratethemtoenhancedevelopment.StartwithcoreReactconceptsanduseState,thengraduallyintroducemorecomplexsolutionslikeReduxorMobXasnee

How React Uses Keys to Identify List Items EfficientlyHow React Uses Keys to Identify List Items EfficientlyApr 28, 2025 am 12:20 AM

Reactuseskeystoefficientlyidentifylistitemsbyprovidingastableidentitytoeachelement.1)KeysallowReacttotrackchangesinlistswithoutre-renderingtheentirelist.2)Chooseuniqueandstablekeys,avoidingarrayindices.3)Correctkeyusagesignificantlyimprovesperformanc

Debugging Key-Related Issues in React: Identifying and Resolving ProblemsDebugging Key-Related Issues in React: Identifying and Resolving ProblemsApr 28, 2025 am 12:17 AM

KeysinReactarecrucialforoptimizingtherenderingprocessandmanagingdynamiclistseffectively.Tospotandfixkey-relatedissues:1)Adduniquekeystolistitemstoavoidwarningsandperformanceissues,2)Useuniqueidentifiersfromdatainsteadofindicesforstablekeys,3)Ensureke

React's One-Way Data Binding: Ensuring Predictable Data FlowReact's One-Way Data Binding: Ensuring Predictable Data FlowApr 28, 2025 am 12:05 AM

React's one-way data binding ensures that data flows from the parent component to the child component. 1) The data flows to a single, and the changes in the state of the parent component can be passed to the child component, but the child component cannot directly affect the state of the parent component. 2) This method improves the predictability of data flows and simplifies debugging and testing. 3) By using controlled components and context, user interaction and inter-component communication can be handled while maintaining a one-way data stream.

Best Practices for Choosing and Managing Keys in React ComponentsBest Practices for Choosing and Managing Keys in React ComponentsApr 28, 2025 am 12:01 AM

KeysinReactarecrucialforefficientDOMupdatesandreconciliation.1)Choosestable,unique,andmeaningfulkeys,likeitemIDs.2)Fornestedlists,useuniquekeysateachlevel.3)Avoidusingarrayindicesorgeneratingkeysdynamicallytopreventperformanceissues.

Optimizing Performance with useState() in React ApplicationsOptimizing Performance with useState() in React ApplicationsApr 27, 2025 am 12:22 AM

useState()iscrucialforoptimizingReactappperformanceduetoitsimpactonre-rendersandupdates.Tooptimize:1)UseuseCallbacktomemoizefunctionsandpreventunnecessaryre-renders.2)EmployuseMemoforcachingexpensivecomputations.3)Breakstateintosmallervariablesformor

Sharing State Between Components Using Context and useState()Sharing State Between Components Using Context and useState()Apr 27, 2025 am 12:19 AM

Use Context and useState to share states because they simplify state management in large React applications. 1) Reduce propdrilling, 2) The code is clearer, 3) It is easier to manage global state. However, pay attention to performance overhead and debugging complexity. The rational use of Context and optimization technology can improve the efficiency and maintainability of the application.

The Impact of Incorrect Keys on React's Virtual DOM UpdatesThe Impact of Incorrect Keys on React's Virtual DOM UpdatesApr 27, 2025 am 12:19 AM

Using incorrect keys can cause performance issues and unexpected behavior in React applications. 1) The key is a unique identifier of the list item, helping React update the virtual DOM efficiently. 2) Using the same or non-unique key will cause list items to be reordered and component states to be lost. 3) Using stable and unique identifiers as keys can optimize performance and avoid full re-rendering. 4) Use tools such as ESLint to verify the correctness of the key. Proper use of keys ensures efficient and reliable React applications.

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor