Refactoring JS code: Let JS code plane

Source: Internet
Author: User

JS in a lot of nesting functions, very cow fork, then why to flatten?

Understand (oneself and others)

Easy to modify (oneself and others)

Normal Ajax invocation notation (based on jquery)

$.post (' url ', Jsonobj,
                function (data) 
                {
                      if (data)
                      {
                           var tips = $.ligerdialog.tip ({title: ' Tip '), Content: ' Operation successful! '});
                           settimeout (function () {tips.close ();}, \ n);
                      }
                      else
                      {
                            var tips = $.ligerdialog.tip ({title: ' Tip ', Content: ' Operation failed! '});
                            settimeout (function () {tips.close ();},%);
                      }
                }
        );

What are the disadvantages?

After the function is nested, it is difficult to understand

function nesting, a line of function calls written a lot of lines, it is easy because of commas, parentheses and so on caused by syntax errors

jquery and Liger in the application code strong coupling, if you want to replace the UI framework, need to do a carpet search ...

Add delay attribute-Deferred

var Ajaxhandler = $.post (' url ', params);
Ajaxhandler.done (checkserverresponse);
    
    
var checkserverresponse=function
{
      if (result)
       {
             var tips = $.ligerdialog.tip ({title: ' Tip '), Content: 

' Operation successful! '});
              settimeout (function () {tips.close ();}, \ n);
        }
        else
        {
                var tips = $.ligerdialog.tip ({title: ' Tip ', Content: 

' Operation failed! '});
                settimeout (function () {tips.close ();},%);
         }
}

Explanation:

The JS after the change is not very different from the previous: If JS uses oo way to write, and then look at this code is very clear (you can simply interpret Checkserverresponse as a private class method, it will be easy to understand the writing intent)
What if there are multiple AJAX requests? Can be written as $.when ($.post (' Url1 '), $.post (' url2 '). Done (this.checkserverresponse); similar to the meaning of merging, and The parameter of the Checkserverresponse function is 2, corresponding to the result of the 2 AJAX requests respectively
What if there are multiple follow-up requests? Can be written as $.when ($.post (URL)). Then (Handler1). Then (Handler2). Done (Successhandler). Always (Alwayshandler). Fail ( Failhandler);

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.