模块化
约 183 字小于 1 分钟
布欧-Lewyon
2026-05-16
首页 › JavaScript › ES6+ 深入 › 模块化
export / import
// math.js — 导出
export const PI = 3.14;
export function add(a, b) { return a + b; }
export class Calculator { }
// 默认导出
export default function multiply(a, b) {
return a * b;
}// app.js — 导入
import multiply, { PI, add, Calculator } from './math.js';
import * as math from './math.js'; // 命名空间导入
console.log(math.PI);
console.log(math.add(1, 2));动态 import
// 按需加载
const module = await import('./heavy-module.js');
module.doSomething();import.meta
console.log(import.meta.url); // 当前模块 URL小结
| 语法 | 用途 |
|---|---|
export | 导出命名声明 |
export default | 默认导出 |
import { } | 按名导入 |
import * as | 命名空间导入 |
import() | 动态导入(返回 Promise) |
上一节:Event Loop 与微任务/宏任务 下一节:Proxy 与 Reflect
