在列表3中,你已看到通过调用函数getReadyStateHandler()创建了一个处理函数,并被配置给onreadystatechange属性。getReadyStateHandler()使用了这样的事实:函数是JavaScript中的主要对象。这意味着,函数可以作为参数被传递到其它函数,并且可以创建并返回其它函数。getReadystateHandler()要做是就是返回一个函数,来检查XMLHttpRequet是否已经完成处理,并传递XML服务器响应到由调用者指定的处理函数。列表6是getReadyStateHandler()的代码。
列表6:函数getReadyStateHandler()
/*
* Returns a function that waits for the specified XMLHttpRequest
* to complete, then passes its XML response to the given handler function.
* req - The XMLHttpRequest whose state is changing
* responseXmlHandler - Function to pass the XML response to
*/
function getReadyStateHandler(req, responseXmlHandler) {
// 返回一个监听XMLHttpRequest实例的匿名函数