Use JavaScript switch case alternative writing _javascript tips

Source: Internet
Author: User
Tags switch case
Accustomed to C # syntax, encountered multiple branch calls, in addition to the simplest if-else, then is switch-case, so the use of JavaScript is very easy to write the following code:
Copy Code code as follows:

JScript Source Code
The value of something is 1, 2, 3 ...
Switch (something) {
Case 1:
Todoa ();
Break
Case 2:
Todob ();
Break
Case 3:
Todoc ();
Break
//...
}

This code itself is nothing, just less readable, looks a bit laborious, JavaScript although support switch-case, but there is a better way to promote:
Copy Code code as follows:

JScript Source Code
The value of something is 1, 2, 3 ...
var cases = {
1:todoa,
2:todob,
3:todoc
};
if (Cases[something]) {
Cases[something] ();
}

Author: Justin
Source: http://justinw.cnblogs.com/
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.