简体   繁体   English

了解jQuery源代码中过于简单的模块的用途

[英]Understanding the Purpose of overly simple Modules in jQuery source

I am trying to read and understand jquery source at their github repo for learning. 我试图在他们的github仓库中阅读和理解jquery的源代码以进行学习。

There I saw some overly simple codes are put into separate modules. 在那里,我看到了一些过于简单的代码被放入单独的模块中。 Say, for example, arr.js at here 这里说例如arr.js

Which contains only the below code, 其中仅包含以下代码,

define( function() {
    "use strict";

    return [];
} );

There must be some usefullness to it as it is in jquery source. 就像在jquery源中一样,它必须具有一定的实用性。

Now, can anyone please explain what is the point of defining a module which only returns an empty array? 现在,任何人都可以解释一下定义仅返回空数组的模块有什么意义吗?

I mean the concept of module is to break down big complex codes into smaller reusable chunks and things like that. 我的意思是模块的概念是将大型复杂的代码分解为较小的可重用块,诸如此类。

Any help? 有什么帮助吗?

Thanks in advance. 提前致谢。

Edit. 编辑。 Some Clarification 一些澄清

For Down voters and those who think this question is 对于唐纳德选民和那些认为这个问题是

opinion-based question and therefore not good for SO 基于观点的问题,因此不利于SO

I am trying to learn from a very popular open source library. 我正在尝试从一个非常流行的开源库中学习。 And there I saw something which I did not clearly understand. 在那儿,我看到了一些我不太清楚的东西。 It might be a best practice, or might be an overall great way of coding . 这可能是最佳实践,或者可能是整体上很好的编码方式 So I asked this question to get more insight/understanding into the code. 因此,我提出了这个问题,以便对代码有更多的了解/理解。 It is a technical question so I asked at SO, [where would I ask if not here?]. 这是一个技术问题,所以我在SO上问,[我在哪里问?

I DID NOT ASK FOR ANYONE'S OPINION 我没有问任何人的意见

If you feel the question is wrongly asked, SO gave everyone the power to edit it, please use it. 如果您认为问题是错误提出的,那么SO赋予了所有人编辑它的权力,请使用它。

If you look at the history of arr.js , you'll see that the original commit describes it as: 如果查看arr.js的历史记录,您会看到原始提交将其描述为:

AMD-ify jQuery AMD-ify jQuery

https://github.com/jquery/jquery/commit/6318ae6ab90d4b450dfadf32ab95fe52ed6331cb https://github.com/jquery/jquery/commit/6318ae6ab90d4b450dfadf32ab95fe52ed6331cb

AMD stands for Asynchronous Module Definition. AMD代表异步模块定义。 The purpose of AMD is to provide modularity in the jQuery code. AMD的目的是在jQuery代码中提供模块化。 Here's a good article that describes the purpose: 这是一篇很好的文章,描述了目的:

When we say an application is modular, we generally mean it's composed of a set of highly decoupled, distinct pieces of functionality stored in modules. 当我们说应用程序是模块化的时,通常是指它由一组高度分离的,不同的功能块存储在模块中组成。 As you probably know, loose coupling facilitates easier maintainability of apps by removing dependencies where possible. 如您所知,松散耦合通过在可能的情况下消除依赖关系来简化应用程序的可维护性。 When this is implemented efficiently, its quite easy to see how changes to one part of a system may affect another. 当有效地实现这一点时,很容易看出对系统某个部分的更改可能会如何影响另一部分。

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

I guess to enforce the "use strict". 我猜想执行“严格使用”。

eg 例如

For this module 对于此模块

define( function() {
    "use strict";

    return window.document;
} );

in non-strict mode you can do window.document = 10 在非严格模式下,您可以执行window.document = 10

In strict mode is an error 在严格模式下是一个错误

Cannot assign to read only property 'document' of object '#' 无法分配为只读对象“#”的属性“ document”

Or it could be just there way of creating small modules that will be easier to refactor later. 或者,也可以只是创建小模块的方式,以便以后进行重构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM