在 Javascript AND 中,为什么定义一个没有名字的模块有用?
In Javascript AMD, why is it useful to define a module without a name?
命名模块对我来说很有意义:
define('myModule', ['dep1', 'dep2'], function (dep1, dep2) {
//Define the module value by returning a value.
return function () {};
});
当我要使用这个模块时,可以使用require
导入:
require('myModule', function(myModule){})
然而,我无法理解的是这样的匿名模块(来自requireJS examples):
define(['dep1', 'dep2'], function (dep1, dep2) {
//Define the module value by returning a value.
return function () {};
});
上面的代码是用来定义匿名模块的吗?如果是这样,这个模块怎么会被其他模块used/imported/refered?有人对此有想法吗?
如果您在链接的页面上向下滚动一点,它会显示
Notice that the above module does not declare a name for itself. This is what makes the module very portable. It allows a developer to place the module in a different path to give it a different ID/name. The AMD loader will give the module an ID based on how it is referenced by other scripts.
所以模块实际上会根据您加载包含它的文件的方式获得一个名称。
我想这个想法是您使用 "anonymous" 个模块(每个文件一个)进行开发,然后有一个将它们全部捆绑在一起的构建工具(在此过程中给它们命名)。
命名模块对我来说很有意义:
define('myModule', ['dep1', 'dep2'], function (dep1, dep2) {
//Define the module value by returning a value.
return function () {};
});
当我要使用这个模块时,可以使用require
导入:
require('myModule', function(myModule){})
然而,我无法理解的是这样的匿名模块(来自requireJS examples):
define(['dep1', 'dep2'], function (dep1, dep2) {
//Define the module value by returning a value.
return function () {};
});
上面的代码是用来定义匿名模块的吗?如果是这样,这个模块怎么会被其他模块used/imported/refered?有人对此有想法吗?
如果您在链接的页面上向下滚动一点,它会显示
Notice that the above module does not declare a name for itself. This is what makes the module very portable. It allows a developer to place the module in a different path to give it a different ID/name. The AMD loader will give the module an ID based on how it is referenced by other scripts.
所以模块实际上会根据您加载包含它的文件的方式获得一个名称。
我想这个想法是您使用 "anonymous" 个模块(每个文件一个)进行开发,然后有一个将它们全部捆绑在一起的构建工具(在此过程中给它们命名)。