JavaScript if default is not the last entry of switch

Source: Internet
Author: User

In other wordsSwitchThe statement should not be familiar with any other types of C language,JavaScriptNaturally, this is also true. The switch logic is very simple. Execute the corresponding case item according to the value of the switch content. Otherwise, execute the default item. However, different languages process different details.

For example, in JavaScript, each case item can have no break, so the statement will be postponed to the next case or default-but some Language designers think this feature is easy to cause deviations in code understanding, for example, in C #, each case must have a break. Then let's look at another detail: if there is a case after default, what will happen? What if there is no break in default?

 
 
  1. switch (a)   
  2. {  
  3. case 0:  
  4. console.log("0");  
  5. default:  
  6. console.log("default");  
  7. case 1:  
  8. console.log("1");  

Just like this code, what content will be output when a is equal to 0, 1, or 2? Let's guess. Don't rush to look down.

When a is equal to 0, the output is:

 
 
  1. 0    
  2. default   

When a is equal to 1, the output is:

 
 

When a is equal to 2, the output is:

 
 
  1. default   

Well, although such code is rare, there is no "special" execution result. The switch rule can still be clarified in one sentence: If a case is matched, It will be executed from the case. Otherwise, it will start from the default place and go down until the break statement appears. Whether or not the default position is at the end of the rule is completely unaffected.

Of course, I really didn't expect anyone to write such code, so if someone feels confused about it, I don't think it's too much. However, since I want to write Jscex, I still have to understand the behavior of such code. Although language users can select a proper subset, the developer compiler, interpreter, and so on of the language must follow the complete specifications, which is a problem for projects such as Jscex.

Since Jscex claims to support "All JavaScript language features", the support for switch is also included. The trouble with the switch is that each of its branches is not as independent as the if statement, but will continue to "penetrate" until it encounters break. Therefore, Jscex also uses some tips when processing the switch. For example, the following code:

 
 
  1. switch (a) {  
  2. case 0:  
  3. $await(helloWorld());  
  4. default:  
  5. console.log("default");  
  6. case 1:  
  7. console.log("1");  

Jscex will "complete" the statements in each case and default to "ensure" that each item has a complete statement and the final break:

 
 
  1. switch (a) {  
  2. case 0:  
  3. $await(helloWorld());  
  4. console.log("default");  
  5. console.log("1");  
  6. break;  
  7. default:  
  8. console.log("default");  
  9. console.log("1");  
  10. break;  
  11. case 1:  
  12. console.log("1");  
  13. break;  

Then compile it:

 
 
  1. switch (a) {  
  2. case 0:  
  3. return $$_builder_$$_0.Bind(helloWorld(), function () {  
  4. console.log("default");  
  5. console.log("1");  
  6. return $$_builder_$$_0.Normal();  
  7. });  
  8. default:  
  9. console.log("default");  
  10. console.log("1");  
  11. return $$_builder_$$_0.Normal();  
  12. case 1:  
  13. console.log("1");  
  14. return $$_builder_$$_0.Normal();  
  15. }  
  16. }) 

Naturally, if the switch does not contain bind operations such as $ await statements, the entire switch statement will be retained, which is also one of the optimization policies for Jscex compilation results.

Original article address:Http://blog.zhaojie.me/2011/05/javascript-when-break-is-not-the-last-choice-of-switch.html

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.