未捕获的类型错误:undefined不是函数
Uncaught TypeError: undefined is not a function
我正试图找出整整一个月内两个日期之间的差异,我在Stack Exchange上找到了一个小脚本,但它一直给我一个错误"Unaught TypeError:undefined不是函数"。
我在chrome中得到了这个,它是一个相当简单的HTML页面。
它似乎不喜欢函数的第一行(var monthsLeft=to.getMonth()等)
(为清晰起见,略有简化:)
<script>
function monthDiff(from, to) {
var monthsLeft = to.getMonth() - from.getMonth() + (12 * (to.getFullYear() - from.getFullYear()));
if(to.getDate() < from.getDate()) {
monthsLeft--;
}
return monthsLeft;
}
function lumpChange() {
var today = new Date();
var april5th = new Date();
var MonthsLeft = 0;
today = '10/05/2015';
april5th = '04/05/2015';
MonthsLeft = monthDiff(today, april5th);
}
</script>
您的monthDiff()
函数看起来似乎希望Date
对象作为参数传递(它使用.getMonth()
等方法)。你在传递字符串。
您不能通过将字符串分配给对象最初所在的变量来修改Date
对象。
您可以在实例化Date
时定义它:
function lumpChange(){
var today = new Date(2015, 10, 05),
april5th = new Date(2015, 04, 05);
MonthsLeft = monthDiff(today, april5th); // -6
}
JSFiddle
如果在实例化Date
对象后需要设置它的日期/时间,可以使用setter方法。
文档:
- Date对象
相关文章:
- 为什么会出现错误;未捕获的类型错误:undefined不是函数;
- 为什么会出现错误,“UncaughtTypeError:undefined不是函数”
- mongoose.connect undefined不是一个函数
- Uncaught TypeError:undefined不是函数-ember js
- jQuery Mobile Undefined不是一个函数
- 未捕获的类型错误:undefined不是函数$
- 使用在函数外部声明的变量的 Javascript 返回值 + undefined
- 为什么Grunt Copy给了我一个'undefined不是函数'警告
- Bootstrap-“;未捕获的类型错误:undefined不是函数;
- Undefined不是函数ImpressPages DatePicker
- 尝试使用.ninnerHTMLlog打印数据,获取错误Uncaught TypeError:undefined不是函数
- 得到“;undefined不是函数“;错误
- Undefined不是Fullcalendar js上的函数
- 使用谷歌地图:一个返回undefined的自定义javascript函数
- jqGrid:undefined不是函数
- Jquery,Uncaught TypeError:undefined不是函数
- $http.post undefined 不是函数
- Ember cli Uncatch TypeError: undefined 不是函数
- Infragistics igGrid Uncatch TypeError: undefined 不是一个函数
- JQuery脚本在Firefox上工作;undefined不是函数“;铬