月中的天数功能只能一次性使用
Days in month function only has single use
我使用下面的函数来确定一个月的天数:
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);
函数和变量名相同
function daysInTheMonth(month,year) {
return new Date(year,month,0).getDate();
}
var test = daysInTheMonth(currYear,currMonth+2);
相关文章:
- 添加文字和评论功能更新Div
- JavaScript打印功能使日历停止工作
- 每当您在选择器内移动鼠标时,悬停功能就会重复
- 如何防止网页加载后自动启动功能
- 除修剪外的其他功能
- Angular:更新一次性绑定的数据
- 悬停功能触发器
- 使用angularjs向浏览器发送servlet响应(下载功能)
- 删除CKEditor工具栏按钮,但不删除功能
- 异步facebook功能
- 如何将chrome扩展功能移植到移动设备(特别是jquery和trello)
- jQuery滚动功能只工作一次
- Graphiti中是否有任何工具提示功能
- React redux初始化功能,无论状态变化如何
- 在哪里可以学习ECMAScript标准中尚未包含的JavaScript功能
- 正在获取select上的功能id
- Rhino打印功能
- 如何使此功能在我的 html 页面中一次性工作
- 角度 ng 禁用一次性绑定功能
- 月中的天数功能只能一次性使用