The Date object has a getDay method, which returns the day of the week in a specific date based on the local time. The return value is from 0~6, corresponding to Sunday~Saturday
getDay0123456
What day Sunday Monday Tuesday Wednesday Thursday Friday Saturday
When using date-related requirements, you need to convert the value returned by getDay to the day of the week, that is, what day of the week is "this day"? For example, after selecting the calendar in the calendar component, return to "2014-12-22 Monday".
This is a piece of code that is still running online
The code copy is as follows:
/*
* Return the day of the week according to the Date object
* @param {Date} date
* @return {String} "Wednesday"
*/
function getChineseWeekByDate(date) {
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 'Wednesday';
} else if (numWeekDay == 4) {
return 'Thursday';
} else if (numWeekDay == 5) {
return 'Friday';
} else if (numWeekDay == 6) {
return 'Saturday';
} else {
return '';
}
}
This code is judged through multiple if else branches and returns to the day of the week. Some students mentioned that it can be optimized using switch.
The code copy is as follows:
/*
* Return the day of the week according to the Date object
* @param {Date} date
* @return {String} "Wednesday"
*/
function getChineseWeekByDate(date) {
var numWeekDay = date.getDay();
switch (numWeekDay) {
case 0: return 'Sunday';
case 1: return 'Monday';
case 2: return 'Tuesday';
case 3: return 'Wednesday';
case 4: return 'Thursday';
case 5: return 'Friday';
case 6: return 'Saturday';
default: return '';
}
}
Compared with if/else, the code is much simpler and shorter. Someone has done a statistical code, the shorter the time the brain thinks. Therefore, you will see various people and books that advocate and praise "short codes" and "The Way of Concise Code".
"Code Collection" mentions the use of table driver method to simplify programming
Table-driven method - Table-driven method is a programming mode that searches information from a table without using logical statements (if and switch). In fact, anything that can be selected through logical statements can be selected by looking up the table. For simple cases, it is easier and more straightforward to use logical statements. But as the logic chain becomes more and more complex, the table lookup method becomes more and more attractive.
As mentioned above, using tables to replace logical statements, many front-end engineers in JS have tried their best to eliminate statements with expressions since they have learned some of the characteristics of functional languages. for example
1. && Replace single if
The code copy is as follows:
if (a == 1) {
$.ajax(xx)
}
// -->
(a == 1) && $.ajax(xx)
2. ?: Substitute if/else
The code copy is as follows:
if (a == 1) {
$.ajax(xx)
} else {
$(yy).remove()
}
// -->
(a == 1) ? $.ajax(xx) : $(yy).remove()
3. Multiple if/else and switch can also be replaced with multiple "?:"
The code copy is as follows:
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 also use functions to recursively eliminate for/while statements. At first I was addicted to these writing methods, but later I found that I couldn't understand it (maybe I still read less, and my brain always naturally converted these into sentences). In the end, I was still used to using sentences.
Let's try to replace the table in "Code Collection" with a JS object.
The code copy is as follows:
/*
* Return the day of the week according to the Date object
* @param {Date} date
* @return {String} "Wednesday"
*/
function getChineseWeekByDate(date) {
var numWeekDay = date.getDay();
var weekObj = {
'0': 'Sunday',
'1': 'Monday',
'2': 'Tuesday',
'3': 'Wednesday',
'4': 'Thursday',
'5': 'Friday',
'6': 'Saturday',
};
return weekObj[numWeekDay] || '';
}
Compared with switch, a lot of code has been reduced, but there are still keys with 0~6. The getDay method returns from 0, just like the JS array index, and also starts from 0. Therefore, using arrays can be simplified
The code copy is as follows:
/*
* Return the day of the week according to the Date object
* @param {Date} date
* @return {String} "Wednesday"
*/
function getChineseWeekByDate(date) {
var numWeekDay = date.getDay();
var weekArr = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
return weekArr[numWeekDay] || '';
}