Home > Web Front-end > JS Tutorial > Use JavaScript switch case alternative writing method_javascript skills

Use JavaScript switch case alternative writing method_javascript skills

WBOY
Release: 2016-05-16 18:32:22
Original
1119 people have browsed it

If you are used to the syntax of C#, when you encounter multi-branch calls, in addition to the simple if-else, it is switch-case, so when using JavaScript, it is easy to write the following code:

Copy code The code is 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;
//...
}

Such code itself is nothing, but less readable , it seems a bit laborious. Although JavaScript supports switch-case, there is a better writing method worth promoting:
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 labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template