JQuery source code analysis note (4) Ready Function

Source: Internet
Author: User

This function mentioned three equivalent forms in jQuery's document:
Copy codeThe Code is as follows:
// Defined in jQuery. fn. ready
$ (Document). ready (handler );
// It is the same as the previous one and is not recommended.
$ (). Ready (handler );
// Process in the jQuery object separately
$ (Handler );
// The preceding definition:
If (jQuery. isFunction (selector ){
Return rootjQuery. ready (selector );
}

Therefore, they all come down to a form: jQuery. fn. ready (fn ). Definition:
Copy codeThe Code is as follows:
Ready: function (fn ){
// Bind the event to the DOM
JQuery. bindReady ();
// Trigger the callback function
ReadyList. done (fn );
// Return the jQuery object
Return this;
}

In fact, jQuery does not only have a reference to fn. The Deferred function is used here. In row 75, the readyList member is defined for the jQuery object. This variable is initialized in the bindReady function in row 442:
Copy codeThe Code is as follows:
If (readyList ){
Return;
}
ReadyList = jQuery. _ Deferred ();

In addition to initializing readyList, The bindReady function mainly handles the differences between browser binding events. IE uses attachEvent while other browsers use addEventHandler. After the two steps are completed, the ready function uses readyList. resolveWith to trigger the callback function. In addition to this work, ready also processes holdReady. The function of this API is to delay the callback of the ready event. The main purpose is to do something before the ready event. HoldReady sets a flag readyWait. When this flag is set, ready continuously calls setTimeout (jQuery. ready, 1) Before calling readyList. resolveWith ). That is, call yourself recursively at a fixed time (I don't know if the js engine will overflow the stack after a long hold time). When holdReady is released, setTimeout will return along the call stack. In order not to trigger the ready callback function before the stack is complete. The readyWait variable is incremented every time setTimeout is called. Used to indicate the number of calls delayed by the holdReady function.


### Basic auxiliary functions
At the beginning of line 1, several notable auxiliary functions are defined: parseJSON, parseXML, and globalEval. ParseJSON converts a string into a JSON object. We generally use eval. ParseJSON encapsulates this operation, but eval is used as the final method. Because JSON serialization and deserialization APIs are added to the latest JavaScript standard. If the browser supports this standard, the two APIs are implemented using Native Code in the JS engine, and the efficiency is certainly much higher than eval. Currently, both Chrome and Firefox4 support this API. ParseJSON uses the following:
Copy codeThe Code is as follows:
// Native json api. Deserialization is JSON. stringify (object)
If (window. JSON & window. JSON. parse ){
Return window. JSON. parse (data );
}
//... Check the validity of the string roughly.
Return (new Function ("return" + data ))();

ParseXML functions are mainly encapsulated by standard APIs and IE. The standard API is a DOMParser object. IE uses the ActiveXObject object of Microsoft. XMLDOM. Definition:
Copy codeThe Code is as follows:
If (window. DOMParser ){
Tmp = new DOMParser ();
Xml = tmp. parseFromString (data, "text/xml ");
} Else {
Xml = new ActiveXObject ("Microsoft. XMLDOM ");
Xml. async = "false ";
Xml. loadXML (data );
}

The globalEval function loads a script into the global context. You can use window.exe cScript in IE. Other browsers need to use eval. Because the entire jQuery code is an anonymous function, the current context is jQuery. Main Code:
Copy codeThe Code is as follows:
(Window.exe cScript | function (data ){
Window ["eval"]. call (window, data); // run in window context
}) (Data );

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.