我正在使用以下功能来确定一个月中的天数:

function daysInMonth(month,year) {
  return new Date(year,month,0).getDate();
}


现在,例如,当我执行以下操作时,它会很好地工作:

var currMonth = currDate.getMonth(),
    currYear = currDate.getFullYear(),
    daysInMonth = daysInMonth(currYear,currMonth+1);


但是,如果我尝试再次使用相同的功能,如下所示:

var test = daysInMonth(currYear,currMonth+2);


我收到以下错误:

Uncaught TypeError: number is not a function


为什么会这样呢?

最佳答案

您正在为变量daysInMonth分配函数daysInMonth的值,从而有效地将函数替换为整数。给您的变量起一个不同的名称,它将起作用,例如

var currMonth = currDate.getMonth(),
currYear = currDate.getFullYear(),
numberOfDaysInMonth = daysInMonth(currYear,currMonth+1);

var test = daysInMonth(currYear,currMonth+2);

08-15 17:57