AMD加载器如何在引擎盖下工作? [英] How do AMD loaders work under the hood?

查看:110
本文介绍了AMD加载器如何在引擎盖下工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我现在一直在使用require.js,但我意识到我实际上并不知道它是如何工作的。它说这是一个AMD加载器。

So I've been using require.js for while now, but I realized that I actually don't know how it works under the hood. It says that it's an AMD loader.

我知道CommonJS是同步的,这意味着它在加载时会阻止其他代码的执行。另一方面,AMD是异步的。这就是我感到困惑的地方。

I do understand that CommonJS is synchronous, which means that it blocks execution of other codes while it's being loaded. On the other hand, AMD is asynchronous. This is where I get confused.

当我定义一个模块时,它必须加载a,b,c才能执行回调。异步如何在这里工作?

When I define a module, it has to load a,b,c in order to execute the callback. How does asynchronous work here?


  1. 当它必须首先加载这三个依赖项时,它不是同步的吗?

  2. 是吗?意味着AMD异步加载a,b,c然后检查是否加载了这些文件(不关心顺序)然后执行回调?





define("name",["a","b","c"], function(a,b,c){

});


推荐答案

如您所知,AMD(异步模块定义) (AMD))是一个特定的API。有许多与AMD兼容的加载器,包括RequireJS,curl.js和Dojo(以及其他)。

As you know, "AMD" (Asynchronous Module Definition (AMD)) is a specific API. There are many AMD-compatible "loaders", including RequireJS, curl.js and Dojo (among others).

就像JQuery和Dojo这样的框架为你提供API原始Javascript;使用AMD的程序:

Just as frameworks like JQuery and Dojo give you an API over raw Javascript; a program that uses AMD:

1)要求你提供与AMD兼容的.js库,

1) requires you an AMD-compatible .js library,

2)需要某些编程规则和约定,并且

2) demands certain programming "rules" and "conventions", and

3)最终位于Javascript的顶部,它运行在你的Javascript引擎上(不管是IE吗? ,Chrome,Firefox - 无论如何)。

3) Ultimately sits "on top" of Javascript, which runs on your "Javascript engine" (be it IE, Chrome, Firefox - whatever).

以下是我发现有用的几个链接:

Here are a couple of links I found useful:

http://dojotoolkit.org/reference-guide /1.8/loader/amd.html

http://blog.millermedeiros.com/amd-is-better-for-the-web-than-commonjs-modules/

http://addyosmani.com/writing-modular-js /

PS:
要回答您的直接问题,后一个链接有点讨论require()和dynamic_loaded依赖。

PS: To answer your immediate question, the latter link has a bit of discussion about "require()" and "dynamically_loaded dependencies".

这篇关于AMD加载器如何在引擎盖下工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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