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

查看:129
本文介绍了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没有 define require 关键字甚至。它们都是规格而不是图书馆。

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()<返回promises而不是模块/ code>或者它可能需要回调。无论哪种方式,它都会使使用大量模块的任何复杂代码变得更加复杂。

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 加载同步模块时,只需加载
模块一次,对的任何其他调用需要整个代码库中的相同模块(针对该进程)返回缓存响应,每次都是相同的对象。代码的任何部分都可以修改该对象,并且它可用于代码的任何其他部分。一些使用该功能的用例实现起来要复杂得多。此外,加载和执行代码的顺序将更难预测。

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天全站免登陆