JavaScript bit by bit jquery

Source: Internet
Author: User

1;(function (Global, Factory) {2FactoryGlobal);3}(typeofWindow!=="undefined"? window: This, Function (window, noglobal) {4     varJQuery =function (Selector, context) {5         return NewJQuery.fn.init (selector, context);6     };7Jquery.fn = Jquery.prototype = {};8     //Core Approach9     //Callback SystemTen     //Asynchronous Queues One     //Data Caching A     //Queue Operations -     //Selector Primer -     //Property Manipulation the     //node Traversal -     //Document Processing -     //Style Actions -     //Property Manipulation +     //Event System -     //Ajax Interaction +     //Animation engine A     returnJQuery; at}));
    1. The first point of any library and frame design is to solve the problem of namespace and variable pollution.
    2. jquery is the use of JavaScript function scope features,
    3. The immediate invocation of an expression wraps itself up to solve the problem.
    4. Both window and undefined are designed to reduce the scope scope of variable lookups.
    5. When a window is passed inside the closure, it is used inside the closure,
    6. You can think of it as a local variable, which is obviously faster than when you were looking under window scope.
    7. Why pass undefined?
    8. Undefined in Javascript is not a keyword, so you can allow users to assign a value to it.

jquery uses () to enclose the anonymous function, followed by a pair of parentheses (including the argument list), then the parentheses can combine our expressions and each block (that is, each pair of parentheses) has a return value. This return value is actually the return value of the expression in parentheses.             So, when we enclose the anonymous function with a pair of parentheses, the actual parentheses return is the function object of the anonymous functions. Therefore, the parenthesis pair plus the anonymous function is like the name of the function that we get its reference position. So if you add a parameter list after the reference variable, the invocation form of the normal function is implemented.

   

1 jquery has 3 ways to load documents2 3 $ (document). Ready (function () {4     // ... Code ...5 })6 //Document Ready Shorthand7 $ (function () {8     // ... Code ...9 })Ten $ (document). Load (function () { One     // ... Code ... A})
(1) parsing the HTML structure. (2) Loading external scripts and style sheet files. (3) Parse and execute script code. (4) Constructs an HTML DOM model. Ready (5) Loads external files such as pictures. (6) The page has finished loading. Load

Ready is executed after step (4) is completed, but load is not executed until step (6) is complete.

 

JQuery.ready.promise =function (obj) {if( !readylist) {Readylist=jquery.deferred (); if(Document.readystate = = =" Complete" ) {            //Handle It asynchronously to allow scripts the opportunity//To delay readysetTimeout (Jquery.ready); } Else{Document.addeventlistener ("domcontentloaded", completed,false ); Window.addeventlistener ("Load", completed,false ); }    }    returnreadylist.promise (obj);}; 
//ensure firing before onload, maybe late but safe also for IFRAMEsDocument.attachevent ("onreadystatechange", completed);//A fallback to window.onload.Window.attachevent ("onload", completed);//If IE and not a frame//continually check to see if the document was readyvartop =false;Try{Top= Window.frameelement = =NULL&&document.documentelement;} Catch(e) {}if(Top &&top.doscroll) {(function Doscrollcheck () {if( !jquery.isready) {Try {                //Use the trick by Diego Perini// http://javascript.nwbox.com/IEContentLoaded/Top.doscroll (" Left"); } Catch(e) {returnSetTimeout (Doscrollcheck, - ); }            //Detach all DOM ready eventsdetach (); //and execute any waiting functionsJquery.ready (); }    })();}

If the browser has an document.onreadystatechange event,

When the event is triggered, it document.readyState=complete can be considered as if the DOM tree is already loaded.

However, this event is unreliable, such as when a picture is present on the page, it may be triggered after the onload event.

Load detection for IE

Diego Perini in 2007, reported a way to detect whether IE is loaded, using DoScroll method invocation, details are visible http://javascript.nwbox.com/IEContentLoaded/.
The principle is that for IE in non-IFRAME, only continuously through can execute doscroll to determine whether the DOM is loaded. In the above interval of 50 milliseconds to try to execute doscroll, note that because the page does not load complete, call DoScroll will cause an exception, so use Try-catch to catch the exception.
Conclusion: So in general, when the page DOM is not loaded, an exception is generated when the DoScroll method is called. Then we use the reverse, if not abnormal, then the page Dom loading is complete.

if (document.readystate = = = "complete") {     //Handle It asynchronously-allow scripts the opportunity-delay read Y     setTimeout (jquery.ready);}

Determine whether the page has been loaded directly by looking at the status of the readystate. This will give a timer a minimum time to execute after, the main guarantee to execute the correct.

Var _jquery = window.jquery,    _$ = window.$;jquery.noconflict = function (deep) {    if (window.$ = = = JQuery) {
   
    window.$ = _$;    } if (deep && window.jquery = = = JQuery) {        window.jquery = _jquery;    }    return jQuery;};
   
    • If we need to use both jquery and other JavaScript libraries, we can use $.noconflict () to give control of the $ to other libraries.
    • The old reference $ is saved in the initialization of jquery; Noconflict () simply restores them.
    • With the concept of swap-like swaps, the previously existing namespaces are cached.
    • By comparing the current namespace to the purpose of the exchange, first, we first determine if the current $ space is being taken over by jquery,
    • If it is then give control to the library referenced by the previous _$, if the incoming deep is true, then the control of jquery is also allowed to go out.

JavaScript bit by bit jquery

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.