Table-driven method-cleverly returning Chinese weeks using Arrays

Source: Internet
Author: User

Table-driven method-cleverly returning Chinese weeks using Arrays
This is a piece of code that is still running online/** returns the day of the week * @ param {Date} Date * @ return {String} "Wednesday" */function getChineseWeekByDate (date) based on the date object) {var numWeekDay = date. getDay (); if (numWeekDay = 0) {return 'sunday';} else if (numWeekDay = 1) {return 'monday ';} else if (numWeekDay = 2) {return 'tuesday';} else if (numWeekDay = 3) {return 'wedday';} else if (numWeekDay = 4) {return 'thursday';} else if (numWeekDay = 5) {return 'Friday' ;} Else if (numWeekDay = 6) {return 'saturday';} else {return '';} This code is determined by multiple if else branches and returns the day of the week, some people mentioned that they can use switch optimization to return the day of the week * @ param {Date} Date * @ return {String} "Wednesday" */function getChineseWeekByDate (date) based on the date object) {var numWeekDay = date. getDay (); switch (numWeekDay) {case 0: return 'sunday'; case 1: return 'monday'; case 2: return 'tues'; case 3: return 'wedned'; case 4: return 'thurs'; case 5: return 'Friday'; ca Se 6: return 'saturday'; default: return '';} the code is simpler and clearer than if/else. The shorter the Statistical Code that someone has done, the shorter the time for the brain to think. Therefore, you will see various "the beauty of short codes", "the simple way of code", and other people and books that advocate and praise "short codes. The table-driven method is a programming mode (scheme) that searches for information from the table without using logical statements (if and switch ). In fact, you can select a logical statement from a table. In simple cases, it is easier and straightforward to use logical statements. However, as the logic chain becomes more complex, the lookup method becomes more attractive. As mentioned above, using tables instead of logical statements, many front-end engineers in JS have tried their best to use expressions to eliminate statements since learning some features of functional languages. For example, 1. & replace single if (a = 1) {$. ajax (xx)} // --> (a = 1) & $. ajax (xx) 2 .?: Replace if/else if (a = 1) {$. ajax (xx)} else {$ (yy ). remove ()} // --> (a = 1 )? $. Ajax (xx): $ (yy). remove () 3. Multiple if/else and switch can also use multiple "?: "Replace if (a = 1) {alert (1)} else if (a = 2) {alert (2)} else if (a = 3) {alert (3)} else {alert (4)} // --> (a = 1 )? Alert (1): (a = 2 )? Alert (2): (a = 3 )? Alert (3): alert (4) In addition, you can use the function recursion to eliminate the for/while statement. I began to become addicted to these writing methods, and later found that I could not understand them (maybe I still read less, the brain always naturally converts these idioms), and finally I used to use statements. The table in code Daquan, we use a JS object to replace/** returns the day of the week * @ param {Date} Date * @ return {String} "Wednesday" */function getChineseWeekByDate (date) {var numWeekDay = date. getDay (); var weekObj = {'0': 'sunday', '1': 'monday', '2': 'tues', '3': 'wedned ', '4': 'thurs', '5': 'Friday', '6': 'satur',}; return weekObj [numWeekDay] | '';} compared to switch, A lot of code is reduced, but there is still 0 ~ The key with 6 Characters exists. The returned value of the getDay method starts from 0 exactly the same as that of the JS array index. Therefore, the array can be used to simplify the process of returning the day of the week * @ param {Date} Date * @ return {String} "Wednesday" */function getChineseWeekByDate (date) based on the date object) {var numWeekDay = date. getDay (); var weekArr = ['sunday', 'monday', 'tues', 'wedned', 'thurs', 'Friday', 'satur']; return weekArr [numWeekDay] | '';}

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.