Summary:
1. The whole class library is defined in an anonymous function, which eliminates the generation of global variables;
2. The undefined is passed as missing parameter to prevent the pollution of the undefined variable;
3. You can see that $ (...) actually returns an instance of the JQuery.fn.init object and then points the object's prototype to the Jquery.prototype (statement jQuery.fn.init.prototype = JQUERY.FN), the resulting instance shares the methods and attributes in Jquery.prototype and realizes the operation of chained programming;
4. Finally through window.jquery = window.$ = jquery, jquery and $ are exported as global variables.
Copy Code code as follows:
(function (window, undefined) {
//Define a local copy of JQuery
VA R JQuery = (function () {
var jQuery = function (selector, context) {
//The jquery object is actually just the Init constructor ' enhanced '
Return to new JQuery.fn.init (selector, context/*, rootjquery*/);
};
//...
Jquery.fn = Jquery.prototype = {
Constructor:jquery,
Init:function (selector, context, rootjquery) {
//...
}
//...
};
//Give the INIT function the JQuery prototype for later instantiation
JQuery.fn.init.prototype = Jquery.fn;
//...
//Expose jquery to the global object
return jquery;
}) ();
//...
Window.jquery = window.$ = JQuery;
}) (window);