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:
// 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:
// 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/