Original address: http://www.cnblogs.com/galenyip/p/4613430.html
Let's take a look at the asynchronous commit of JS.
XHR we often use it when we are native, because it is often used, we have more of it encapsulated in the tool library
Let's look at his most common implementations.
1 //Old Method2 3 functioncreatexhr () {4 varxhr;5 Try{6XHR =NewXMLHttpRequest ();7}Catch(e) {8 Handleerr (e);9 Ten Try { OneXHR =NewActiveXObject ("Msxml2.xmlhttp"); A}Catch(e) { - Try{ -XHR =NewActiveXObject ("Microsoft.XMLHTTP"); the}Catch(e) { -XHR =NULL; - } - } + } - + returnxhr; A } at - functionHandleerr (Error) { - //This step is important in combat because the catch extends the scope chain, so it is declared at the global scope by the E - //Here we assign it to local variables, then find faster - varErr =error; - in //Do sth. -}
Are you sure? This is the most common method of CREATEXHR implementation.
that
Title, what is the inert function here to say?
Let's first say what it does: optimize the functions that are often called.
This is also part of JS Advanced
Not much to say, directly on the code
1 //Lazy Function2 //The second time only takes effect .3 //role: Optimization for frequently used functions4 functioncreatexhr () {5 varxhr;6 if(typeofXMLHttpRequest! = ' undefined ') {7XHR =NewXMLHttpRequest ();8CREATEXHR =function() {9 return NewXMLHttpRequest ();Ten } One}Else { A Try { -XHR =NewActiveXObject ("Msxml2.xmlhttp"); -CREATEXHR =function() { the return NewActiveXObject ("Msxml2.xmlhttp"); - } -}Catch(e) { - Try { +XHR =NewActiveXObject ("Microsoft.XMLHTTP"); -CREATEXHR =function() { + return NewActiveXObject ("Microsoft.XMLHTTP"); A } at}Catch(e) { -CREATEXHR =function () { - return NULL; - } - } - } in } - returnXHR to}
Code, we let the function after the first run, then we will be judged in addition to the browser environment, is re-assigned value. The assigned function is a direct return corresponding to the method.
So, this function needs to be called a second time before it is actually called.
It is because it called the function for the second time, did not go to the first call that complex judgment of the road, so appear "lazy." So we call it an inert function .
Lazy function--js Advanced