Next, return to the definition of ready (fn) in the first jQuery. extend function (row 268. This function is used to process events completed by DOM loading. It is almost one of jQuery's most commonly used functions. This function mentioned three equivalent forms in jQuery's document:
The 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:
The 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:
The 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:
The 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:
The 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:
The Code is as follows:
(Window.exe cScript | function (data ){
Window ["eval"]. call (window, data); // run in window context
}) (Data );