Example of how to use a command object to replace the switch statement and switch statement

Source: Internet
Author: User
Tags case statement

Example of how to use a command object to replace the switch statement and switch statement

Someone once said that a really good program does not have if... else. Of course, the switch is not as good as if... else. Switch is forbidden in JS specifications.

The command object solves this problem perfectly.

Reference a foreign blog:

JavaScript has good control flow statements, which are often enclosed in curly brackets. But there is one exception: switch... Case statement. Switch... The strange thing about case is that you must add the keyword break at the end of each case to prevent process control from traversing into the next case statement. Traversal refers to the method for executing multiple cases. When the expected break is not met, the control is automatically handed over to the next case. However, just like semicolons and curly braces, you are likely to forget to write break inadvertently. When this happens, troubleshooting will be painful in the future, because the statement itself is correct. Therefore, write case in pairing... Break is a good habit.
We usually say that JavaScript has an elegant object literal and top-level function, which makes the query of specific methods very simple. An object created for method query is called an action object or command object. It is used in many software design modes, including powerful and useful command modes.

Instance:

Copy codeThe Code is as follows:
// Switch method
Function testSwitch (name ){
Switch (name ){
Case '1 ':
Return 'hink ';
Break;
Case '2 ':
Return 'slash ';
Break;
Case '3 ':
Return 'run ';
Break;
Default:
Return false;
Break;
}
}
// Use the command object
Function testFn (name ){
Var names = {
'1': function (){
Return 'hink ';
},
'2': function (){
Return 'slash ';
},
'3': function (){
Return 'run ';
}
};
If (typeof names [name]! = 'Function '){
Return false;
}
Return names [name] ();
}
// Test Result
Var result1 = testSwitch ('1 ');
Var result2 = testFn ('2 ');
Console.info (result1, result2 );

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.