How to write elegant and readable JavaScript code

Source: Internet
Author: User
Tags readable

Even if is the simplest requirement, and different programmers will write code that is not the same:

Requirements: Top-up procedures are not eligible to recharge the amount of money, that can only be charged to 100, 200, 500, 1000 amount, other worry:

1. Novice programmers may write this, though readable, code-verbose, and not elegant.

$ (function () {            var recharge = +;            if (Recharge | | | recharge | | | = = Recharge | | recharge = +) {//to do                some thing!            }        });

2. Veteran programmers may write this, the code is concise, but the readability is weak.

1 $ (function  () {2             var recharge = +; 3 4             if (/1000| ([1,2,5][0]{1,2})/g.test (Recharge)) {5                 //to dosome thing! 6             }7         });

3. The recommended wording is this, both elegant and readable.

$(function() {Array.prototype.inArray=function(func, item) {varCur = This;  for(varIdxinchcur) {                    if(func (Cur[idx], item)) {return true; }                }                return false;            }; varRecharge = 100; if([1000].inarray,function(i, cur) {returni = =cur; }, Recharge)) {//To do some thing!            }        });

How to write elegant and readable JavaScript code

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.