Module

Organizes the code in logical or functional units.

The jQuery library resides in a single Module with about 10 thousand lines of code.



Overview


The Module pattern really stands out in jQuery because the entire code base resides in a single module. You can verify this by looking at the first line and then scrolling down to the very last line in the source file. The entire body of code is wrapped in a single immediate function (or IIFE) which makes up the module. Here are the relevant lines:

(function (window, undefined) {

  // ~ 10,000 lines of code ...

})( window );

The window argument is the global object. By passing it as an argument it becomes a locally available variable ensuring fast access. The parameter named undefined is to prevent hackers from redefining the built-in 'undefined' value.

All this is 'vanilla' Module and has been discussed in our Module discussion in the Moderns Patterns section.