javascript - 为什么有同步和异步模块的规范? [英] javascript - Why is there a spec for sync and async modules?

查看:19
本文介绍了javascript - 为什么有同步和异步模块的规范?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚读完这篇文章 在 Javascript 模块上.我可以理解,CommonJS 模块是同步加载的,而 AMD 模块是异步加载的.

I have just finished reading this article on Javascript modules. I can understand that CommonJS modules are synchronously loaded while AMD modules are asynchronously loaded.

我不明白的是,如果我以 CommonJS 格式编写模块,我怎么能神奇地同步,或者如果我把它写在AMD 格式. 我的意思是 javascript 甚至没有 definerequire 关键字.它们只是规格而不是库.

What I don't understand is that how can I module become magically synchronous if I write it in the CommonJS format, or how it becomes magically async if I write it in the AMD format. I mean javascript does not have a define or require keyword even. All they are are specs not libraries.

我的意思是模块加载的行为取决于模块加载器,而不是模块的结构.如果是这样,为什么要为不同类型的模块遵循编码模式?

I mean the behaviour of module loading is dependent on the module loader and not how the module is structured. And if that is the case why follow a coding pattern for different types of modules ?

我是否正确地假设 NodeJS 世界中的所有库都是同步加载的,无论它们是以什么格式编写的.并且浏览器空间中的所有模块都是异步加载的.

如果我的上述假设是正确的,那么为什么还有 UMD 的规范?我的意思是,如果脚本是根据它所在的环境加载的,那么为什么要为通用模块加载制定规范?

If my above assumption is correct then why is there even a spec for UMD ? I mean if a script loads based on the environment it is present in then why make a spec for universal module loading ?

有人能帮我解决这个困惑吗?

Can someone help me with this confusion ?

推荐答案

这是个好问题.这是一个在 Node 社区引起了很多热烈讨论的话题.为了更好地理解它的全部内容,您应该阅读:

This is a good question. It's a subject that caused a lot of heated discussion in the Node community. To have a good understanding of what it's all about you should read:

  • Node.js, TC-39, and Modules by James M Snell from iBM
  • ES6 Module Interoperability - Node.js Enhancement Proposals
  • In Defense of .js - A Proposal for Node.js Modules by Dave Herman, Yehuda Katz and Caridy Patiño
  • Discussion on the Pull Request #3 of node-eps (002: ES6 module interop)

现在,回答您的问题 - 为什么有同步和异步模块的规范?因为有些用例更喜欢模块的同步加载,比如 Node.js 中的服务器端模块,你想在开始服务请求之前加载你需要的一切,而有些用例更喜欢异步加载模块,比如在浏览器中,当你不不想在加载依赖项时阻塞渲染线程.

Now, answering your question - Why is there a spec for sync and async modules? Because some usecases prefer the synchronous loading of modules, like the server-side modules in Node.js where you want to load everything you need before you start serving requests, and some usecases prefer asynchronous loading of modules, like in the browser when you don't want to block the rendering thread while you load the dependencies.

在浏览器中确实没有同步加载的选项,因为它会使浏览器没有响应.

There is really no option for synchronous loading in the browser because it would make the browser not responsive.

您可能会争辩说您可能会在服务器上使用异步加载,但是您要么必须通过 require() 返回承诺而不是模块,否则它可能需要回调.无论哪种方式,它都会使使用大量模块的任何复杂代码变得更加复杂.

You could argue that you might use asynchronous loading on the server but then you'd either have to return promises instead of modules by require() or it could take callbacks. Either way it would make any complex code that uses a lot of modules much more complicated.

另一个问题是已加载模块的缓存和变异.通过使用 require 同步模块加载,您只需加载模块一次以及对整个代码库中相同模块(对于该进程)的任何其他 require 调用返回缓存响应,每次都是相同的对象.代码的任何部分都可以修改该对象,并且它可用于代码的任何其他部分.一些使用该功能的用例实施起来要复杂得多.此外,加载和执行代码的顺序将更难预测.

Another issue is with the caching and mutation of the already loaded modules. With synchronous module loading using require you only load the module once and any other calls to require for the same module in the entire code base (for that process) return a cached response, which is the same object every time. Any part of the code can modify that object and it is available to any other part of the code. Some usecases that use that feature would be much more complex to implement. Additionally the order of loading and executing code would be harder to predict.

总结一下您的问题的答案,两种加载模块的方式都有争议,而且这两种方式都不是每个场景的明显赢家.两者都是必需的,并且都有一些规范来规范其行为.

To sum up the answer to your question, there are arguments for both ways of loading modules and neither of those ways is a clear winner for every scenario. Both are needed and both have some specs to standardize their behavior.

阅读我链接的文章以获得更详细的理解.

Read the articles that I linked for more detailed understanding.

这篇关于javascript - 为什么有同步和异步模块的规范?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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