ES6 module

本文深入探讨了ES6模块系统的特性,包括静态结构、同步及异步加载方式、循环依赖的支持等,并介绍了如何使用导出与导入语法来组织代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Overview

  • A preference for single exports and support for cyclic dependencies.
  • Support asynchronous loading and configurable module loading.
  • Bundle structure could be statically analyzed for static checking, optimization etc.

The ES6 module standard has two parts:
- Declarative syntax for importing and exporting.
- Programmatic loader API: to configure how modules are loaded and to conditionally load modules.

Module Syntax

There are two kinds of exports: named exports (several per module) and default exports (one per module)

Named Exports

A module can export multiple things with the key word export and distinguished by their names .

// lib.js
export const constant = 'constant'
function funcA () {}
function funcB () {}
export { funcA, funcB }

// main.js
import { constant, funcA, funcB } from 'lib.js'
// or you could import everything
import * as lib from 'lib.js'
Default Exports

ES6 allows to export a default value for one module (file), and then import it with a name.

// lib.js
export default function () {}
// main.js
import Func from 'lib.js'
Mixing named exports and default export in a module

It’s a common usage to have both named exports and default export in a module in the case that we want to export a standard interface as default export and also exports multiple specific values in the meanwhile.

// lib.js
function funcA () {}
function funcB () {}
let all = { funcA: funcA, funcB: funcB }
export default all
export { funcA, funcB }

// main.js
import all, { funcB } from 'lib.js'

Notice that the default export is actually a named export with the special name default.

import { default as all } from 'lib.js'
// equivalent to 
import all from 'lib.js'

const constant = 'constant'
export default constant
// equivalent to
export { constant as default }

Advantages

Static Module Structure

ES6 enforces a static structure, which means import and export must be determined at compile time. You can not import modules conditionally like if...else....

Synchronous And Asynchronous Loading

Since ES6 enforces static module structure, by default modules are synchronous loaded and executed. Conditional importing and asynchronous resolving (it means using import directive here) is prohibited. ES6 provides import() method for asynchronous loading and resolving, which return a promise for afterwards snippets when it’s loaded and resolved.

Support For Cyclic Dependencies

ES6 modules export bindings, not values, and therefore connection to variables declared inside module body keeps alive. This is the point quite different from commonJS, which return the instance (value) of the exporting module and every time you import it, it remains the values declared inside the module body though you change it in functions.

Circular dependencies

Circular dependencies is that importing each other in files, which is the different situation from cyclic dependencies. You might find it not work the way you want if you run across circular dependencies.

// moduleA.js
import { b } from 'moduleB.js'
console.log('b: ' + b)
const a = 'a'
console.log('module A executed')
export { a }

// moduleB.js
import { a } from 'moduleA.js'
console.log('a: ' + a)
const b = 'b'
console.log('module B executed')
export { b }

// entry main.js
import { b } from 'moduleB.js'

// b: undefined (Error Reference)
// module A executed
// a: a
// module B executed
CommonJS、AMD 和 ES6 Module 是 JavaScript 中三种不同的模块化规范,用于实现代码的模块化和复用。 1. **CommonJS**: - CommonJS 是一种同步加载模块的规范,主要用于服务器端(如 Node.js)。 - 使用 `require` 导入模块,使用 `module.exports` 或 `exports` 导出模块。 - 示例: ```javascript // 导入模块 const moduleA = require('moduleA'); // 导出模块 module.exports = { functionA: function() { // ... } }; ``` 2. **AMD (Asynchronous Module Definition)**: - AMD 是一种异步加载模块的规范,主要用于浏览器端。 - 使用 `define` 定义模块,使用 `require` 加载模块。 - 示例: ```javascript // 定义模块 define(['moduleB'], function(moduleB) { return { functionB: function() { // ... } }; }); // 加载模块 require(['moduleB'], function(moduleB) { moduleB.functionB(); }); ``` 3. **ES6 Module**: - ES6 Module 是 ECMAScript 6 引入的官方模块化规范,既适用于浏览器端也适用于服务器端。 - 使用 `import` 导入模块,使用 `export` 导出模块。 - 示例: ```javascript // 导入模块 import { functionC } from 'moduleC'; // 导出模块 export function functionC() { // ... } ``` ### 对比 - **同步 vs 异步**: CommonJS 是同步加载,适用于服务器端;AMD 是异步加载,适用于浏览器端;ES6 Module 可以在编译时进行静态分析,既支持同步也支持异步。 - **语法**: CommonJS 使用 `require` 和 `module.exports`,AMD 使用 `define` 和 `require`,ES6 Module 使用 `import` 和 `export`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值