This project has a function. switch statements are frequently used. During code optimization, replace them with arrays.
1. Simple Example:
Switch case
The Code is as follows:
Var caseContent = ''; // content after condition judgment
Var caseValue = 5; // condition judgment Value
Switch (caseValue ){
Case 0:
CaseContent = "Shoes ";
Break;
Case 1:
CaseContent = "Pants ";
Break;
Case 2:
CaseContent = "coat ";
Break;
......
Case 5:
CaseContent = "hat ";
Break;
Default:
CaseContent = "casual ";
Break;
}
Array use case
The Code is as follows:
Var caseContent = ''; // content after condition judgment
Var caseValue = 5; // condition judgment Value
Var caseContentArr = ["Shoes", "Pants", "coat"..., "Hats"];
CaseContent = caseContentArr [caseValue]? CaseContentArr [caseValue]: "casual ";
2. Advantages and Disadvantages Analysis
The code in array mode is concise and efficient, but the readability is not as good as switch case.