JQuery's function design is very compact and there are many calls to each other. For example, you need to understand function a and find that function B is called in function a, and Function c is called in function B. In this way, the system will basically be dizzy.
Therefore, I suggest reading a function and a function. Do not follow it up. After reading all the articles, you will naturally get through.
So today I will annotate jQuery constructor.
Function jQuery (a, c ){
// Process the ready function, $ (function (){})
If (a & a. constructor = Function & jQuery. fn. ready)
Return jQuery (document). ready ();
// Set a default value for
A = a | jQuery. context | document;
// If a is a jQuery object, merge a and null arrays and return the result. The purpose of this operation is not to destroy the original jQuery object.
// (Note: The jquery attribute is available for each jQuery object and its value is the jQuery version)
If (a. jquery)
Return $ (jQuery. merge (a, []);
// If c is a jQuery object, call the find function to find
If (c & c. jquery)
Return $ (c). find ();
// None of the above cases. Start the new jQuery object.
If (window = this)
Return new jQuery (a, c );
// If a is an html code, $ ('<div/>') converts the html code into a Dom element.
// JQuery. clean converts html code into a Dom element array.
Var m =/^ [^ <] * (<. +>) [^>] * $/. exec ();
If (m) a = jQuery. clean ([m [1]);
// If array a is an array or an array of classes containing dom elements, merge array a and null.
// In other cases, call the find function. The find function is used to process css expressions.
// Call the get method to return the jQuery object.
This. get (a. constructor = Array | a. length &&! A. nodeType & a [0]! = Undefined & a [0]. nodeType?
// Assume that it is an array of DOM Elements
JQuery. merge (a, []):
// Find the matching elements and save them for later
JQuery. find (a, c ));
// Check whether the last parameter is a Function. If yes, traverse and execute it.
Var fn = arguments [arguments. length-1];
If (fn & fn. constructor = Function)
This. each (fn );
}
Finally, from the above constructor code, jQuery constructor mainly supports the following methods:
1. $ (Function), ready Function
2. $ (Element)/$ ([Element]) can directly convert Dom elements or arrays into jQuery objects.
3. $ (CSS Expression, Content). You can also use CSS expressions to select Dom elements.
4. $ (Html) and html can also be converted to jQuery objects.
Author: baozhifei