In Jquery.js's constructor, the dynamic nature of the Javsscript language is fully utilized--the strict requirement of the type and number of the row parameter, so that a function can realize a variety of functional requirements, but also provides the basis for the JavaScript language polymorphism, in this constructor, Six different invocation formats are available (according to the official API documentation), as follows ($ = jQuery):
1, $ (String expr): Finds matching elements, such as $ ("Div>p"), based on the given CSS selector;
2, $ (element Elem): Converts the given DOM element object to a jquery object, such as $ (document). Find ("div>p");
3, $ (array<element> Elems): such as $ (myform.elements). Hide ();
4, $ (function fn): $ (document). Ready () Shorthand mode, such as: $ (function fn () {...}) ;
5, $ (jQuery obj): such as: var div = $ ("div"); $ (DIV). Find ("P");
6, $ (String expr, Element context): Find expr in context such as: $ ("div", xml.responsexml);
In addition, jquery mentions the idea of chainable methods, that is, invoking the method in jquery will return a jquery object, still can continue to invoke the method in it, thus forming a "chain", through the "." A call goes down, this in the constructor has a concrete embodiment, which has the following statement:
if (window = = this) return new JQuery (A, c);
This is to return a jquery object, when the first call to the jquery (a, c) function, this is equal to window, so each time a jquery object is created, more detailed code analysis is shown below:
The constructor of jquery;
var jQuery = function (A, c) {
$ (document). The shorthand form of ready () is executed only under $ (function () {});
if (a && typeof a = = "function" && JQuery.fn.ready) return JQuery (document). Ready (a);
Make sure parameter a is not empty and the default value is document;
A = a | | Jquery.context | | Document
If parameter A is a jquery object (a.jquery= "1.0.3"), a jquery object that is the same as a is cloned;
if (a.jquery) return jquery (Jquery.merge (A, []));
Find a in the given parameter C (which requires C to be a jquery object);
if (c && c.jquery) return jquery (c). find (a);
If it is the first call to $ (), because in the window environment, so create a new jquery object, if you remove new, loop execution;
if (window = = this) return new JQuery (A,C);
Parsing HTML strings, such as "div<ul>p";
if (A.constructor = = String) {
var m =/^[^<]* (<.+>) [^>]*$/.exec (a);
if (m) a = Jquery.clean ([m[1]]);
}
If the parameter a is an array of elements, the Jqery.merge () is executed, otherwise the jquery.find () is executed;
This.get (A.constructor = = Array | | a.length &&!a.nodetype && a[0]! = undefined && a[0].nodetype
? Working with array of elements;
Jquery.merge (A, [])
://Find matched elements and save;
Jquery.find (A, c));
If additional functions are attached, the function is executed on each matching jquery object;
var fn = arguments[Arguments.length-1];
if (fn && typeof fn = = "function") This.each (FN);
return this;
}; The end of jquery;
jquery Source Code Analysis-a detailed construction function