对于 AMD 模块(例如 RequireJs 或 curl.js),我的理解是:
require()
用于异步加载不同的模块,加载完成后执行回调函数。
要定义一个模块,您需要使用 define()
分离脚本。
但是我看到一些模块在其函数定义内部使用了 require()
,例如:
define([a, b, c], function(i, ii, iii){
require([d, e, f], function(d, e, f) {
// do some stuff with these require()'d dependancies
})
/* rest of the code for this module */
})
但我觉得这很令人困惑,因为我原本认为如果一个模块有依赖关系,那么它们应该通过主要的define([dependancies], fnDefinition)
函数传递,而不是像上面的示例一样在其内部通过require()
传递。
这背后是否有理由呢?