JQuery Source Analysis Notes (4) Ready function _jquery

Source: Internet
Author: User
Tags eval
This feature mentions three equivalent forms in the jquery documentation:
Copy Code code as follows:

Defined in JQuery.fn.ready
$ (document). Ready (handler);
And the previous one is the same, not recommended
$ (). Ready (handler);
Handled separately in the jquery object
$ (handler);
The definition of the above form:
if (jquery.isfunction (selector) {
return Rootjquery.ready (selector);
}

So actually all boil down to one form: JQuery.fn.ready (FN). The definition is as follows:
Copy Code code as follows:

Ready:function (FN) {
Binding events to the DOM
Jquery.bindready ();
Trigger callback function
Readylist.done (FN);
Back to the JQuery object
return this;
}

In fact, there is not just one reference to FN within jquery. The deferred function is used here. In line 75, the Readylist member is defined for the jquery object. In 442 rows, this variable was initialized in the Bindready function:
Copy Code code as follows:

if (readylist) {
Return
}
Readylist = jquery._deferred ();

In addition to initializing readylist, the Bindready function mainly handles the browser's differences in binding events. IE uses attachevent while other browsers use addEventHandler. After these two steps are completed, the Ready function uses Readylist.resolvewith to trigger the callback function. In addition to this work, ready also handled the Holdready. The function of this API is to delay the callback of the Ready event, primarily to do something before the Ready event. Holdready set a flag bit readywait. When this flag bit is set, ready calls SetTimeout (Jquery.ready, 1) before calling Readylist.resolvewith. That is, every fixed time on the recursive call themselves (do not know hold time, JS engine will stack overflow), so that the final release by Holdready, SetTimeout will be back along the call stack. To not trigger the ready callback function until this stack completes. The readywait variable is incremented each time the settimeout is invoked. Used to indicate that a few calls were delayed by the Holdready function.


# # #几个基础辅助函数
Starting at line 543, several notable auxiliary functions are defined: Parsejson,parsexml and globaleval. Parsejson a string into a JSON object. We are generally using eval. Parsejson encapsulates this operation, but Eval is treated as a last resort. The API for JSON serialization and deserialization has been added to the latest JavaScript standards. If the browser supports this standard, then these two APIs are implemented in native code in the JS engine, and the efficiency is certainly much higher than the eval. For now, both Chrome and FIREFOX4 support this API. Parsejson is used as follows:
Copy Code code as follows:

Native JSON API. Deserialization is json.stringify (object)
if (window. JSON && windows. Json.parse) {
return window. Json.parse (data);
}
// ... Check the string legitimacy roughly
Return (The New Function ("return" + Data)) ();

The Parsexml function is also mainly the standard API and the encapsulation of IE. Standard APIs are Domparser objects. And IE uses the ActiveXObject object of Microsoft.XMLDOM. Defined:
Copy Code code 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. Window.execscript can be used in IE. Other browsers need to use Eval. Because the entire jquery code is an entire anonymous function, the current context is jquery. Main code:
Copy Code code as follows:

(Window.execscript | | function (DATA) {
window["eval"].call (window, data); Run in window context
}) (data);

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.