diff --git a/docs/documentation/zh/handbook-v2/Modules.md b/docs/documentation/zh/handbook-v2/Modules.md index 7ab4503..aca9929 100644 --- a/docs/documentation/zh/handbook-v2/Modules.md +++ b/docs/documentation/zh/handbook-v2/Modules.md @@ -1,61 +1,56 @@ --- -title: Modules +title: 模块 layout: docs permalink: /zh/docs/handbook/2/modules.html -oneline: "How JavaScript handles communicating across file boundaries." +oneline: "JavaScript 处理跨文件通信的方式。" --- -JavaScript has a long history of different ways to handle modularizing code. -Having been around since 2012, TypeScript has implemented support for a lot of these formats, but over time the community and the JavaScript specification has converged on a format called ES Modules (or ES6 modules). You might know it as the `import`/`export` syntax. +JavaScript 历来具有多种处理代码模块化的方式。TypeScript 自 2012 年问世以来,已经实现了对这其中很多格式的支持。但随着时间的推移,社区和 JavaScript 规范已经趋于使用一种称为 ES 模块(或 ES6 模块)的格式。它使用的是 `import`/`export` 语法。 -ES Modules was added to the JavaScript spec in 2015, and by 2020 had broad support in most web browsers and JavaScript runtimes. +ES 模块在 2015 年被添加到 JavaScript 规范中,并且截至 2020 年已经在大多数 Web 浏览器和 JavaScript 运行时中得到广泛支持。 -For focus, the handbook will cover both ES Modules and its popular pre-cursor CommonJS `module.exports =` syntax, and you can find information about the other module patterns in the reference section under [Modules](/docs/handbook/modules.html). +本手册将重点介绍 ES 模块及其流行的前身 CommonJS `module.exports =` 语法,你可以在参考部分的 [模块](/docs/handbook/modules.html) 下找到其他模块模式的信息。 -## How JavaScript Modules are Defined +## JavaScript 模块的定义方式 -In TypeScript, just as in ECMAScript 2015, any file containing a top-level `import` or `export` is considered a module. +在 TypeScript 中(与 ECMAScript 2015 一样),任何包含顶级 `import` 或 `export` 声明的文件都被视为一个模块。 -Conversely, a file without any top-level import or export declarations is treated as a script whose contents are available in the global scope (and therefore to modules as well). +相反,如果一个文件没有任何顶级导入或导出声明,它将被视为一个脚本,其内容在全局范围内可用(因此也可用于模块)。 -Modules are executed within their own scope, not in the global scope. -This means that variables, functions, classes, etc. declared in a module are not visible outside the module unless they are explicitly exported using one of the export forms. -Conversely, to consume a variable, function, class, interface, etc. exported from a different module, it has to be imported using one of the import forms. +模块在它们自己的作用域中执行,而不是在全局作用域中执行。这意味着在模块中声明的变量、函数、类等在模块外部是不可见的,除非它们使用其中某种导出形式进行了显式导出。相反,要使用从其他模块导出的变量、函数、类、接口等,必须使用某种导入形式进行导入。 -## Non-modules +## 非模块文件 -Before we start, it's important to understand what TypeScript considers a module. -The JavaScript specification declares that any JavaScript files without an `import` declaration, `export`, or top-level `await` should be considered a script and not a module. +在开始之前,我们有必要了解一下 TypeScript 将什么当作模块。JavaScript 规范声明,任何没有 `import` 声明、`export` 声明或顶级 `await` 的 JavaScript 文件都应被视为脚本而不是模块。 +在脚本文件中,声明的变量和类型处于共享的全局作用域。你应该要么使用 [`outFile`](/tsconfig#outFile) 编译器选项将多个输入文件合并为一个输出文件,要么在 HTML 代码中使用多个 `