前言: 使用这种注释方法,在其他地方调用这个函数,鼠标停在这个函数上面时,就是显示这些注释,是一种 十分必要的 规范。
使用 Alt + Shift + A 可以快速生成 /**/ ,最好是自己配置 Snippet
效果:
例子:
/**
* @describe 判断时候是闰年
* @param year 年份
* @returns 是或者否
*/
export function isLeapYear(year: number): boolean {
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
}
/**
* @describe 获取距离某个时间点相差的天数
* @param date 目标日期
* @param start 开始日期
* @returns 天数
*/
export function getDays(date: Date, start?: Date): number {
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
let days = day;
const monthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
for (let i = 0; i < month - 1; i += 1) {
days += monthDays[i];
}
// 如果是闰年,天数加一
if (isLeapYear(year) && month > 2) {
days += 1;
}
if (start) {
return days - getDays(start);
}
return days;
}