模块和脚本有什么区别?

What is the difference between a module and a script?

我正在尝试通过 typescript 文档来理解与 ES6 模块相同的模块。

typescript-modules - typescript 模块的文档。它说

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.

In TypeScript, just as in ECMAScript 2015, any file containing a top-level import or export is considered a module. 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).

它说文件中没有导入或导出语句的任何内容在全球范围内可用。但事实并非如此。

script1.js

var variable = "Hello";

script2.js

console.log(variable);

根据文档中写的语句,当我运行 script2.js时,它不应该给出任何错误并控制变量的值,因为script1.js没有导入,导出语句,因此变量在全局范围内可用。但它给出了一个错误。那么脚本的内容在全局范围内可用是什么意思?

在 HTML 文件中,如果您执行 <script src="./script2.js" /><script src="./script1.js" />,您将在控制台中看到 Hello