3.1 Source code
Init:function (selector, context, rootjquery) {var match, Elem, ret, doc;//Handle $ (""), $ (null), or $ (undefined) Suppose selector is a space.! Selector for Falseif (!selector) {//At this time this is an empty jquery object return this;} Handle $ (domelement)//nodetype node type. Take advantage of whether the NodeType attribute is used to infer if the DOM element is the IF (selector.nodetype) {//The first element and attribute context is pointed to Selectorthis.context = this[0] = Selec Tor;this.length = 1;return This;} The BODY element is only exists once, optimize finding it//because the body appears only once. Use!context to optimize if (selector = = = "Body" &&!context && document.body) {//context point to Document Object th Is.context = Document;this[0] = Document.body;this.selector = Selector;this.length = 1;return this;} Handle HTML Stringsif (typeof selector = = = "string") {//is we dealing with HTML string or an ID?//starts at < with > ending with a length greater than or equal to 3. This is assumed to be an HTML fragment, skipping the queckexpr regular check if (Selector.charat (0) = = = < && Selector.charat (selector.length-1) = = = ">" && selector.length >= 3) {//Assume that strings, start and end with <> is HTML and skip the regex Checkmatch = [NULL, selector, NULL];} else {match = quickexpr.exec (selector);} Verify a match, and that no context is specified for #idif (match && (match[1] | |!context)) {//HANDLE: $ (h TML), $ (array) if (Match[1]) {context = Context instanceof JQuery? context[0]: Context;doc = (context?
context.ownerdocument | | context:document)//If a single string was passed in and it's a single tag//just do a createelement and skip the RESTR ET = rsingletag.exec (selector); Suppose it is a separate label if (ret) {//Assuming the context is a normal object if (Jquery.isplainobject (context)) { The reason is placed in the array. It is convenient to call the Jquery.merge () method later selector = [Document.createelement (ret[1])]; Call the Attr method. Incoming argument ContextjQuery.fn.attr.call (selector, context, true);} else {selector = [Doc.createelement (ret[1])];} How to handle complex html} else {ret = Jquery.buildfragment ([match[1]], [doc]), selector = (ret.cacheable? Jquery.clone (Ret.fra gment): ret.fragment). ChildNodes;} Return Jquery.merge (this, selector);//HANDLE: $ ("#id")} else {elem = document.getElementById (match[2]);//Check Paren Tnode to catch when Blackberry 4.6 returns//nodes that is no longer in the document #6963if (Elem && Elem.paren tnode) {//Handle The case where IE and Opera return items/by name instead of ID//even Documen.getelementbyid such a core approach also takes into account browser compatibility issues that may The name is found instead of Idif (Elem.id!== match[2]) {return rootjquery.find (selector);} Otherwise, we inject the element directly into the jQuery objectthis.length = 1;this[0] = elem;} This.context = Document;this.selector = Selector;return this;} HANDLE: $ (expr, $ (...)) No context specified, run Rootjquery.find (). The context is developed and the context is a jquery object, running Context.find ()} else if (!context | | context.jquery) {return (context | | rootjquery). FIND (Selec TOR);//HANDLE: $ (expr, context)//(which is just equivalent to: $ (context). Find (expr)//assumes that the context is specified. And the context is not a jquery object} else {//First create a jquery object that includes a context. Then call the Find method return This.constructor (context). Find (selector);} HANDLE: $ (function)//Shortcut for document ready} else if (Jquery.isfunction (selector)) {return Rootjquery.ready ( selector);} Selector is the jquery object if (selector.selector!== undefined) {This.selector = Selector.selector;this.context = Selector.context;} Merge to the current JQuery object return Jquery.makearray (selector, this);},
The other jquery functions that are called inside are then specifically learned later.
1, the use of the two is the form of the expression:
(1) quickexpr =/^ (?
: [^#<]* (<[\w\W]+>) [^>]*$|# ([\w\-]*) $)/;
Explanation:?: The meaning of the non-capturing group. The non-capturing group only participates in matching. However, the matching content is not captured in the group, which reduces the memory footprint.
[^#<]* matches random characters except #<, and occurs 0 or more times
(<[\w\W]+>):
\w means matching alphanumeric, underscore, \w just the opposite, here is a label with an alphanumeric underscore as the first letter, for example <div>
[^>]* matches all characters except >, and occurs 0 or more times, $ end, indicating that the end cannot be a > character
# (\w\-]*) $ #id的匹配. ID can only be letters, numbers, underscores, and minus signs
(2) Rsingletag =/^< (\w+) \s*\/?
> (?:<\/\1>) $/;
Explain:^< (\w+) \s*\/?
:
^<: Starting with <
(\w+): Matched letters, numbers, underscores
\s*: Match whitespace 0 or more times, for example to match <div > this
\/?>: Match/Sign 0 or 1 times. For example, matching This
(?:<\/\1>): \1 represents the previous (), which refers to (\w+), for example, the front is <div>. It's going to be </div>
The last? $ indicates that this element is a cutoff, either in </div>, or not in front of the or
This is where all the individual labels are considered completely.
Apply
<script type= "Text/javascript" > var quickexpr =/^ (?: [^#<]* (<[\w\W]+>) [^>]*$|# ([\w\-]*) $)/; var Rsingletag =/^< (\w+) \s*\/?> (?:<\/\1>)? $/; var match = quickexpr.exec ("abc<div> #id"); if (match) { alert (regexp.$1) } var match1 = rsingletag.exec ("<div></div>"); if (match1) { alert (regexp.$1); } </script>
JQuery Source Code Learning Note: jQuery.fn.init (selector,context,rootjquery) code specific explanation