jquery Constructor init parameter analysis (III.)

Source: Internet
Author: User

After parsing the string, there is not much left.

5. Parameter selector is a function

This is very easy to think of, first of all to say DOM loading. We usually have to wait for the document to be loaded when we write the script in the head, JS is so written

1 window.onload=function() {2   your code ... 3 }


However, it is necessary to wait for all resources to be loaded before executing, and if the page has a lot of pictures it will load slowly. Dom2 level There is a load complete event domcontentload more useful, this event will be loaded in the HTML document structure is triggered. For example, this page has 10 img elements, as long as the acquisition of these elements and do not care whether the picture is loaded in this will be a good way to improve the JS loading speed, use the following:

1  function  2     Your code3  false);

Unfortunately, ie low-version browser does not support this way of writing, first of all, ie low version does not addeventlistener this binding event method, but there is a similar method addevent but because there are many problems are often not used, Furthermore, the low version of IE has no domcontentload event. So for IE low version (IE9) it is usually used doscroll combined with onreadystatechange method detection. Refer to http://www.iefans.net/ie-mofang-domcontentloaded/for details

When we're done, back to jquery, we don't have to worry about browser compatibility when we use jquery directly:

1 $ (function() {2   yourcode ... 3 })45// or 67 $ (document). Ready (  function() {8    yourcode ... 9 })

The first is what we're going to say. Selector is a function, in fact jquery will adjust the Ready method, the source code is as follows:

1 // HANDLE: $ (function) 2         // Shortcut for document ready 3         Else if (Jquery.isfunction (selector)) {4             return Rootjquery.ready (selector); 5         }
Rootjquery has said before that is $ (document), and ultimately the ready method of execution This is why the DOM loading of two ways, about the Readdy method is also used in the previous analysis of the Domcontentload,doscroll, onReadyStateChange and so on will only be more complicated to learn after the DOM loading module is slowly analyzed.

6.selector is a jquery object or special object

This kind of situation should not be much. No one likes it. $ (document) write it, but if anyone wants to write jquery, it supports

1 if (Selector.selector!== undefined) {2     this. selector = selector.selector; 3     this. Context = Selector.context; 4 }

If selector exists selector attribute description is probably a jquery object, why is it possible, see the following example to understand

1  Console.log ($ ({selector:1}));

When I write this it is not selector (at this time the object {selector:1}) of the Selector property exists AH and finally will produce results

function function, jquery: "1.7.1" ...] 011__proto__: jquery[0]

This is a correction to the author of the original book, not only jquery objects are handled here

If all the things that were said before are not consistent with that, then jquery has a last resort.

return  this);

The Makearray method converts an array object of class to a group object. But there are even more daunting tasks within the code to convert some arrays, ordinary objects of class arrays, and so on, into jquery form instead of jquery. This method is analyzed in detail later. Here we go. The parameters of the jquery constructor init are analyzed. There are 7 subdivisions of the big class with 12 classifications. Take a picture of the original book and summarize it.

The following is the source code for the entire init function 1.7.1

1Init:function(selector, context, rootjquery) {2         varmatch, Elem, ret, doc;3          4         //Handle $ (""), $ (null), or $ (undefined)5         if( !selector) {6             return  This;7         }8 9         //Handle $ (domelement)Ten         if(selector.nodetype) { One              This. Context = This[0] =selector; A              This. length = 1; -             return  This; -         } the  -         //the BODY element is only exists once, optimize finding it -         if(Selector = = = "Body" &&!context &&document.body) { -              This. Context =document; +              This[0] =document.body; -              This. selector =selector; +              This. length = 1; A             return  This; at         } -  -         //Handle HTML Strings -         if(typeofselector = = = "string" ) { -             //is we dealing with HTML string or an ID? -             if(Selector.charat (0) = = = "<" && Selector.charat (selector.length-1) = = = ">" && selector.length &G T;= 3 ) { in                 //assume that strings, start and end with <> is HTML and skip the regex check -Match = [NULL, selector,NULL ]; to  +}Else { -Match =quickexpr.exec (selector); the             } *       $             //Verify A match, and that no context is specified for #idPanax Notoginseng             if(Match && (match[1 | | |!)context)) { -  the                 //HANDLE: $ (HTML), $ (array) +                 if(match[1] ) { AContext = ContextinstanceofJquery? Context[0]: Context; theDoc = (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 rest $RET =rsingletag.exec (selector); -  -                     if(ret) { the                         if(Jquery.isplainobject (context)) { -selector = [Document.createelement (ret[1] ) ];WuyiJQuery.fn.attr.call (selector, context,true ); the  -}Else { Wuselector = [Doc.createelement (ret[1] ) ]; -                         } About  $}Else { -ret = Jquery.buildfragment ([match[1] ], [Doc]); -Selector = (ret.cacheable?Jquery.clone (ret.fragment): ret.fragment). childNodes; -                     } A  +                     returnJquery.merge ( This, selector); the  -                 //HANDLE: $ ("#id") $}Else { theElem = document.getElementById (match[2] ); the  the                     //Check parentnode to catch when Blackberry 4.6 returns the                     //nodes that is no longer in the document #6963 -                     if(Elem &&elem.parentnode) { in                         //Handle the case where IE and Opera return items the                         //by name instead of ID the                         if(Elem.id!== match[2] ) { About                             returnRootjquery.find (selector); the                         } the  the                         //Otherwise, we inject the element directly into the JQuery object +                          This. length = 1; -                          This[0] =Elem; the                     }Bayi  the                      This. Context =document; the                      This. selector =selector; -                     return  This; -                 } the  the             //HANDLE: $ (expr, $ (...)) the}Else if(!context | |context.jquery) { the                 return(Context | |rootjquery). Find (selector); -  the             //HANDLE: $ (expr, context) the             //(which is just equivalent to: $ (context). Find (expr) the}Else {94                 return  This. Constructor (context). Find (selector); the             } the  the         //HANDLE: $ (function)98         //Shortcut for document ready About}Else if(Jquery.isfunction (selector)) { -             returnRootjquery.ready (selector);101         }102 103         if(Selector.selector!==undefined) {104              This. selector =Selector.selector; the              This. Context =Selector.context;106         }107 108         returnJquery.makearray (Selector, This );109},

jquery Constructor init parameter analysis (III.)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.