ES6模块同步或异步加载? [英] ES6 Modules are loaded Synchronously or Asynchronously?

查看:837
本文介绍了ES6模块同步或异步加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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



我想确认此信息是否正确。

解决方案

ES6模块装载机将是异步的,而node.js模块加载程序不是。



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



< blockquote>

  • 模块代码自动以严格模式运行,无法选择退出严格模式。


  • 在模块顶层创建的变量不会自动添加到共享的全局范围。它们只存在于模块顶级范围
    之内。


  • 是未定义的。不允许在代码中使用HTML样式的评论(早期浏览器日期的
    的剩余功能)。


  • 模块必须导出任何应该



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


模块一般来说为开发人员解决了几个问题。首先,
它们允许开发人员将代码分成较小的块,称为
模块。第二,它们使开发人员轻松地将这些模块加载(注入)
到其他代码段。使模块注入
有助于保持项目代码与模块脱钩(读取:
提高可测试性)。 第三,模块可以异步加载脚本
这意味着应用程序可以开始加载更快,因为
它们不需要在执行代码之前加载所有脚本。 / p>

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



另一方面因为 node.js 是基于 require 这是同步的,这意味着 node.js 不提供开箱即用的异步变体。



当然有节点的异步模块加载器( async-require ),但本机(需要)不支持。


Module loader is responsible for loading modules.

What I know is module loader loads modules in browser asynchronously whereas in Node.js it loads synchronously.

I wanted to confirm whether this information is correct or not.

解决方案

ES6 module loaders will be asynchronous while node.js module loaders are not.

Here are some key aspects of module loaders:

  • 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

On the other hand because node.js is based on require which is synchronous this means node.js does not provide an asynchronous variant out of the box.

Of course there are async module loaders for node (async-require), but natively (with require) is not supported.

这篇关于ES6模块同步或异步加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆