jQuery

JQuery is a library that encapsulates common JavaScript operations. Simplifies HTML manipulation, event handling, animation, and Ajax interactions.

closure

The jquery entry function is a function that executes immediately, and the formation of JavaScript closures is dependent on the scope of the variable, essentially the lexical scope of the variable’s life cycle. Variables are destroyed when they are no longer in use, but remain in memory for as long as they are memorized.

Closure benefits:

  1. You can “private variables” that do not need to be exposed globally.
  2. You can extend the declaration cycle of internal variables

Disadvantages:

  1. Too many privatized variables can cause memory leaks, which I don’t think is a problem in general. The main reason for the memory leak is the circular reference caused by the use of closures. If the DOM node is stored in the scope chain of closures, the memory leak is caused because reference counting is the garbage collection mechanism used in Internet Explorer, and most browsers now use token counting. The solution to this problem is to assign the used variable to NULL after the use is complete.
/ * * *@param  {[object]} global  window 
 * @param  {[function]} factory jquery
 */
(function (gloal, factory) {
    if (typeof module= = ='object' && typeof module.exports === 'object') {module.exports = global.document ? fcatory(global.true) :function (w) {
            if(! w.document) {throw new Error('jquery requires a window with document')}return    factory(w)
        }
    } else {
        factory(global)}}) (typeof window! = ='undefined' ? window : this.function (window, noGlobal){})Copy the code

This code is easy to understand, passing in two parameters window, jquery method.

if (typeof module === 'object' && typeof module.exports === 'object'){ module.exports = global.document ? fcatory(global, true): function (w) { if (! w.document) { throw new Error('jquery requires a window with document') } return factory(w) } }Copy the code

This code implements the common. Js specification. If it does, export the factory and pass it to the window. If not, rquire(‘jquery’) will pass it to the window. Otherwise, an error is thrown. Finally there is the direct fatory(window) that HTML introduces to jquery.js.