JavaScript 中的经典脚本与模块脚本 [英] Classic scripts vs. module scripts in JavaScript
问题描述
我正在浏览 WHATWG 的 async
规范和defer
标签的 属性,当我看到这个语句时:
I was going through the WHATWG specifications for async
and defer
attributes for the <script>
tag, when I saw this statement:
经典脚本可以指定defer
或 async
;模块脚本可以指定async
.
Classic scripts may specify
defer
orasync
; module scripts may specifyasync
.
我浏览了经典和模块脚本的WHATWG定义,但我并没有真正弄明白.简单来说,JavaScript 中的经典和模块脚本有什么区别?
I went through the WHATWG definitions for classic and module scripts, but I didn't really get much clarity. In simple terms, what are the differences between classic and module scripts in JavaScript?
推荐答案
经典脚本 只是您所知的标准 JavaScript 脚本.模块脚本是包含ES6模块,即它使用(或:可以使用)import
和 export
声明.
A classic script is just a standard JavaScript script as you know it. A module script is one that contains an ES6 module, i.e. it uses (or: can use) import
and export
declarations.
来自 §8.1.3.8 集成使用 JavaScript 模块系统:
JavaScript 规范定义了模块的语法,以及他们的处理模型的一些与主机无关的部分.本规范定义其处理模型的其余部分:模块系统如何引导,通过 script
元素和 type
属性设置为"module"
,以及如何获取、解析和执行模块.[JAVASCRIPT]
The JavaScript specification defines a syntax for modules, as well as some host-agnostic parts of their processing model. This specification defines the rest of their processing model: how the module system is bootstrapped, via the
script
element withtype
attribute set to"module"
, and how modules are fetched, resolved, and executed. [JAVASCRIPT]
注意: 尽管 JavaScript 规范是用脚本"和模块"来表述的,但总的来说,这个规范是用脚本"和模块"来表述的经典脚本的条款与模块脚本相比,因为它们都使用脚本元素.
Note: Although the JavaScript specification speaks in terms of "scripts" versus "modules", in general this specification speaks in terms of classic scripts versus module scripts, since both of them use the script element.
另请参阅https://blog.whatwg.org/js-modules.
这篇关于JavaScript 中的经典脚本与模块脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!