I have an annoying bug in on a webpage:
(我在网页上有一个烦人的错误:)
date.GetMonth() is not a function
(date.GetMonth()不是函数)
So, I suppose that I am doing something wrong.
(因此,我想我做错了什么。)
The variable date
is not an object of type Date
.(可变date
不是Date
类型的对象。)
How can I check for a datatype in Javascript?(如何检查Javascript中的数据类型?)
I tried to add a if (date)
, but it doesn't work.(我试图添加一个if (date)
,但是它不起作用。)
function getFormatedDate(date) {
if (date) {
var month = date.GetMonth();
}
}
So, if I want to write defensive code and prevent the date (which is not one) to be formatted, how do I do that?
(因此,如果我想编写防御性代码并阻止格式化日期(不是一个日期),该怎么做?)
Thanks!
(谢谢!)
UPDATE: I don't want to check the format of the date, but I want to be sure that the parameter passed to the method getFormatedDate()
is of type Date
.
(更新:我不想检查日期的格式,但是我想确保传递给方法getFormatedDate()
的参数的类型为Date
。)
ask by Martin translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…