JQuery source code analysis-constructor details, jquery Constructor
In jQuery. js constructor makes full use of the dynamic nature of the JavsScript language-there is no strict requirement on the type and number of row parameters, so that a function can meet multiple functional requirements, it also provides the basis for JavaScript language polymorphism. In this constructor, six different calling formats are provided (according to the official API documentation), as shown below ($ = jQuery ):
1. $ (String expr): searches for matched elements based on the given CSS selector, for example, $ ("div> p ");
2. $ (Element elem): converts a given DOM Element object to a jQuery object, for example, $ (document). find ("div> p ");
3. $ (Array <Element> elems): for example, $ (myForm. elements). hide ();
4. $ (Function fn): It is a short mode of $ (document). ready (), for example, $ (function fn (){...});
5. $ (jQuery obj): for example, var div =$ ("div"); $ (div). find ("p ");
6. $ (String expr, Element context): Search for expr in context, for example, $ ("div", xml. responseXML );
In addition, the idea of Chainable Methods is mentioned in jQuery, that is, calling Methods in jQuery will return a jQuery object and you can still call the Methods in it. In this way, it forms a "chain" through ". it is called one by one, which is embodied in the constructor, including the following statement:
If (window = this) return new jQuery (a, c );
This is to return a jQuery object. this is equivalent to window when jQuery (a, c) function is called for the first time. Therefore, a jQuery object is created every time, for more detailed code analysis, see:
// JQuery constructor;
Var jQuery = function (a, c ){
// $ (Document). ready () is short for execution only under $ (function;
If (a & typeof a = "function" & jQuery. fn. ready) return jQuery (document). ready ();
// Ensure that parameter a is not null and the default value is document;
A = a | jQuery. context | document;
// If parameter a is a jQuery object (a. jquery = "1.0.3"), clone a jQuery object that is the same as;
If (a. jquery) return jQuery (jQuery. merge (a, []);
// Search for a from the given parameter c (requires that c must be a jQuery object;
If (c & c. jquery) return jQuery (c). find ();
// If $ () is called for the first time, a new jQuery object is created in the window environment. If new is removed, it is cyclically executed;
If (window = this) return new jQuery (a, c );
// Analyze HTML strings, such as "div <ul> p ";
If (a. constructor = String ){
Var m =/^ [^ <] * (<. +>) [^>] * $/. exec ();
If (m) a = jQuery. clean ([m [1]);
}
// If parameter a is an element array, run jQery. merge (); otherwise, run jQuery. find ();
This. get (a. constructor = Array | a. length &&! A. nodeType & a [0]! = Undefined & a [0]. nodeType
? // Process the element array;
JQuery. merge (a, [])
: // Search for matched elements and save them;
JQuery. find (a, c ));
// If another function is attached, execute this function on each matching jQuery object;
Var fn = arguments [arguments. length-1];
If (fn & typeof fn = "function") this. each (fn );
Return this;
}; // End of jQuery;