Singleton

An object of which only a single instance can be created.

The entire jQuery library is implemented as a Singleton object.



Overview


Earlier we discussed the Module pattern in jQuery, which, as you know, is JavaScript's manifestation of the Singleton pattern. The jQuery code base lives in a single Module which executes only once. Here are the relevant lines:

(function (window, undefined) {

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

})( window );

This ensures that a web page will have at most one instance of jQuery (or its alias $). As an aside, you can have multiple versions of jQuery running on a page with the help of the jQuery.noConflict() method.



Iterator