javascript - ES6 模块是同步加载还是异步加载?

标签 javascript node.js module ecmascript-6

模块加载器负责加载模块。

我所知道的是模块加载器在浏览器中异步加载模块,而在 Node.js 中它是同步加载的。

我想确认这个信息是否正确。

最佳答案

ES6 模块加载器将是异步的,而 node.js 模块加载器则不是。

以下是模块加载器的一些关键方面:

  • Module code automatically runs in strict mode and there’s no way to opt-out of strict mode.

  • Variables created in the top level of a module are not automatically added to the shared global scope. They exist only within the top-level scope of the module.

  • The value of this in the top level of a module is undefined. Does not allow HTML-style comments within the code (a leftover feature from the early browser days).

  • Modules must export anything that should be available to code outside of the module.

https://leanpub.com/understandinges6/read#leanpub-auto-modules

Modules, in general, solve several problems for developers. First, they allow the developer to separate code into smaller pieces, called modules. Second, they make it easy for developers to load (inject) those modules into other sections of code. Having modules injected like this helps keep project code uncoupled from the module (read: improved testability). And third, modules can load scripts asynchronously. This means that apps can begin loading faster, as they don’t require all scripts to be loaded prior to executing code.

http://chimera.labs.oreilly.com/books/1234000001623/ch03.html#_default_values

另一方面,因为 node.js 基于同步的 require 这意味着 node.js 不提供异步变体盒子外面。

当然有用于 Node 的异步模块加载器(async-require),但不支持 native (需要)。

关于javascript - ES6 模块是同步加载还是异步加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31807799/

相关文章:

javascript - 如何测试@Input()项: any in Angular?

javascript - Mapbox 访问 token - 我将其放置在哪里?

javascript - EJS渲染参数含义

javascript - TypeError : Crafty. 场景不是函数

dependency-injection - DI、构造函数注入(inject)、模块、设计模式

Python - randrange() 的空范围 (0,0, 0) 和 ValueError ("empty range for randrange() (%d,%d, %d)"% (istart, istop, width))

javascript - 使用 AngularJS 单击搜索结果时将输入重置为初始值

javascript - 在 XSLT 中使用变量应用模板

node.js - 如何抓取网络以查找围绕某个主题的链接/网站?

perl - 如何使用 Perl 在 Windows 上将 WMF 图像转换为 PNG 或 JPG?