Lazy function--js Advanced

Source: Internet
Author: User

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

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.