javascript RequireJS 中的命名模块与未命名模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19720646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 16:36:13  来源:igfitidea点击:

Named module vs Unnamed module in RequireJS

javascriptrequirejs

提问by Aniket

We can create a module in requireJS by giving it a name:

我们可以通过给它一个名字来在 requireJS 中创建一个模块:

define("name",[dep],function(dep) {
      // module definition
});

or we can create one excluding the name:

或者我们可以创建一个不包括名称的:

define([dep],function(dep) {
      // module definition
});

Which is the better way to create a module? I know RequireJS recommends to avoid assigning module names.

哪个是创建模块的更好方法?我知道 RequireJS 建议避免分配模块名称。

But I want to know in what scenarios we do and do not have to give a module a name. Does this affect usage? What are the pros and cons of each way?

但是我想知道我们在哪些场景中可以做并且不必给模块命名。这会影响使用吗?每种方式的优缺点是什么?

采纳答案by Louis

This is what the requirejs documentationsays on the topic of named modules:

这是 requirejs文档关于命名模块的内容:

These are normally generated by the optimization tool. You can explicitly name modules yourself, but it makes the modules less portable -- if you move the file to another directory you will need to change the name. It is normally best to avoid coding in a name for the module and just let the optimization tool burn in the module names. The optimization tool needs to add the names so that more than one module can be bundled in a file, to allow for faster loading in the browser.

这些通常由优化工具生成。您可以自己明确命名模块,但这会降低模块的可移植性——如果您将文件移动到另一个目录,您将需要更改名称。通常最好避免在模块名称中编码,而让优化工具在模块名称中刻录。优化工具需要添加名称,以便可以将多个模块捆绑在一个文件中,以便在浏览器中更快地加载。

But let's say you wantyour module to have a single well-known name that allows always requiring it in the same way from any other module. Do you then need to use the definecall with a name? Not at all. You can use pathsin your configuration:

但是,假设您希望您的模块具有一个众所周知的名称,以便始终以与任何其他模块相同的方式要求它。那么您是否需要使用define带有名称的调用?一点也不。您可以paths在配置中使用:

paths: {
   'jquery': 'external/jquery-1.9.1',
   'bootstrap': 'external/bootstrap/js/bootstrap.min',
   'log4javascript': 'external/log4javascript',
   'jquery.bootstrap-growl': 'external/jquery.bootstrap-growl',
   'font-awesome': 'external/font-awesome'
},

With this configuration, jQuery can be required as "jquery", Twitter Bootstrap as "bootstrap", etc. The best practice is to leave calling definewith a name to the optimizer.

使用此配置,jQuery 可能需要 as "jquery"、Twitter Bootstrap as"bootstrap"等。最佳实践是将调用define的名称留给优化器

回答by redmallard

Off the top of my head, the only module I've seen that uses the named module method is jQuery. Personally, I would also recommend using the unnamed module pattern.

在我的脑海里,我见过的唯一一个使用命名模块方法的模块是 jQuery。就个人而言,我还建议使用未命名模块模式。

Named Modules

命名模块

By naming a module, the name used to require is independent of its location. Another module author could blindly require 'jquery', without knowing the modules path. This is useful if you're developing a reusable module (outside of your project). However, it has the drawback that (using jQuery as an example), you would need to add and entry in the pathsconfig property for RequireJS to point to the correct location of the file. Otherwise RequireJS can't find the file to load, unless it is placed in your base require directory.

通过命名模块,用于 require 的名称与其位置无关。另一个模块作者可能会盲目地要求 'jquery',而不知道模块路径。如果您正在开发可重用的模块(在您的项目之外),这将非常有用。但是,它的缺点是(以 jQuery 为例),您需要在pathsRequireJS的config 属性中添加和条目以指向文件的正确位置。否则 RequireJS 找不到要加载的文件,除非它放在你的基础 require 目录中。

Unnamed Modules

未命名的模块

I find these to be preferable because you can take advantage of relative require paths. Let's say you have two files modules/ClassAand modules/ClassB. ClassBcan require ClassAusing './ClassA', and the entire directory can be moved if needed. By using an unnamed module, you also eliminate the possibility of a naming collision, unless you manage to find a way to place two files in the same directory, with the same name.

我发现这些更可取,因为您可以利用相对需要的路径。假设您有两个文件modules/ClassAmodules/ClassB. ClassB可能需要ClassAusing './ClassA',如果需要,可以移动整个目录。通过使用未命名的模块,您还可以消除命名冲突的可能性,除非您设法找到一种方法将两个文件放在同一目录中,并具有相同的名称。