date.gettime不是构造函数
在一个函数中,我有一行:
const currentTime = new Date.getTime();
代码编译正好。
另外, eslint 均未显示警告或
错误。
但是当我运行代码时,我会得到一个警报框,使人感到困惑
消息 date.getTime不是构造函数
。
这里有什么问题?
我注意到,如果我不调用函数,没有错误 其中包含上面的线。
以下堆栈摘要重现了错误。
function rightNow() {
const currentTime = new Date.getTime();
return currentTime;
}
console.log(rightNow().toString());
// ^^ No error if I comment out the previous line! ^^
.as-console-wrapper { max-height: 100% !important; top: 0; }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1。快速修复
只需在
新日期之后立即添加括号
:2。说明
date
。
片段
说 aha!我想我明白了你的意思:有一个班级
date.getTime
,现在您要调用默认构造函数创建一个新对象:
new Date.getTime()
,对吗?好吧,对不起,但是我找不到任何这样的构造函数。
,我可以听到您 - 程序员 - 尖叫否!那不是不是
我的意思! (愚蠢的✴!☠#@☠$%编译器!)
在您冷静下来以恢复合理正常的情况下
血压,您可能(耐心地)尝试以下操作:
编译器先生,我们显然脚下的脚 - 我从来没有
打算伤害你的感情。
我想要的是:请创建一个新的
date
对象,然后致电该新创建的对象上的
getTime()
方法。看,让我按以下方式说,看看您是否更好地理解我的
视角?
丢失'()'调用构造函数new-parens
编译器先生的回应:
或 - 在更人性化的范围中。
好!因此,您要创建一个新的
date
对象?,在该对象上,您要
.getTime()
?很好。 - 除了您忘了告诉我哪个
您要我使用的构造函数。是默认构造函数吗?
或其他一些构造函数?
既然你没有告诉我,我会假设你
要我使用默认构造函数。
,但是下次问我时,您会考虑更明确吗
这样做吗?
这样,我就不必对您的意思做出有根据的猜测。
叹了口气后,您 - 程序员 - 程序员 - 再试一次:
最后 - 编译器先生没有投诉!
这次,编译器先生沉默地回应,这很好
消息!
- 沉默的回应表明,编译器先生处于最佳状态
情绪。
不过,您是人类,而不是编译器,所以您可以想象
编译器先生说:
好的。因此
(new Date()).getTime()
表示您要创建一个新的日期
使用默认构造函数对象,然后调用getTime()
在该对象上。一切都很好。我没有抱怨。,但是由于您是一个非常敏感的人,所以您无法自助
问:
好!我很高兴我们终于同意这一点。但是有什么吗
还在你的想法吗? 。
请原谅我的
问 您的代码很有意义。没关系。确实是。
但是,既然,既然您一直在na缩 - 写
(new Date())
很好。没有错。但是你不需要周围环境
括号。一旦您编写
new Date()
,我就知道您想要创建一个日期对象,即使您然后在此调用方法
对象。
不是愚蠢你知道!
我
-
比编译器错误更难理解和调试
警告。
这是堆栈溢出问答可能会有所帮助的时候。
正如评论中指出的那样,一个较短的替代方案
new Date()。getTime()
是普通而简单date.now()
。这是没有错误的堆栈片段。
参考
1. Quick fix
Just add parentheses immediately after
new Date
:2. Explanation
Date.getTime is not a constructor
If the compiler were a thinking human being, it could respond to the
snippet
by saying Aha! I think I get what you mean: there is a class named
Date.getTime
and now you want to call the default constructorto create a new object:
new Date.getTime()
, right?Well, sorry, but I could not find any such constructor.
And I can hear you – the programmer – screaming No!! That's not
what I meant! (Stupid ✴!☠#@☠$% compiler!)
After you have chilled down enough to restore a reasonably normal
blood pressure, you might – patiently – try something like:
Mr Compiler, we obviously got off on the wrong foot – I never
meant to hurt your feelings.
What I want is: please create a new
Date
object, and then callthe
getTime()
method on that newly created object.Look, let me put it as follows, to see if you better understand my
point of view?
Missing '()' invoking a constructor new-parens
The response by Mr Compiler :
Or – in a more human langage.
OK! So you want to create a new
Date
object?And on that object you want to
.getTime()
?That's fine. – Except that you forgot to tell me which
constructor you want me to use. Is it the default constructor?
Or some other constructor?
Since you didn't tell me, I will assume you
want me to use the default constructor.
But would you consider being more explicit the next time you ask me
to do something like this?
That way I would not have to make an educated guess on what you mean.
After resigning with a sigh, you – the programmer – try again:
Finally – no complaints from Mr Compiler!
This time around, Mr Compiler responds with silence, which is good
news!
– Responding with silence shows that Mr Compiler is in his very best
mood.
Still, you are a human and not a compiler, so you could imagine
Mr Compiler saying:
OK. So
(new Date()).getTime()
means you want to create a newDate
object using the default constructor, and then callgetTime()
on that object. It's all fine. I have no complaints.But since you are a very sensitive human, you cannot help yourself
asking:
Good! I'm glad we finally agree on this. But is there something
still on your mind? Forgive me for asking.
Upon which you imagine Mr Compiler responding:
No no. Your code makes sense. It's OK. It really is.
But well, since you keep nagging me – writing
(new Date())
is fine.There is nothing wrong with it. But you don't need the surrounding
parentheses. Once you write
new Date()
then I understand that youwant to create a Date object, even if you then call a method on that
object.
– I am not stupid you know!
Epilogue
As a general rule, runtime errors – like in this question – are often
much harder to understand and debug than compiler errors and
warnings.
This is when a Stack Overflow question-and-answer might be helpful.
As pointed out in the comments, a shorter alternative to
new Date().getTime()
is the plain and simpleDate.now()
.Here is a Stack Snippet without the error.
Reference