date.gettime不是构造函数

发布于 2025-01-19 05:45:41 字数 1054 浏览 0 评论 0 原文

在一个函数中,我有一行:

const currentTime = new Date.getTime();

代码编译正好。 另外, eslint 均未显示警告或 错误。
但是当我运行代码时,我会得到一个警报框,使人感到困惑 消息 date.getTime不是构造函数

这里有什么问题?

“错误消息:'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; }

In a function I have the line:

const currentTime = new Date.getTime();

The code compiles just fine.
Also, ESLint displays neither warnings or
errors.
But when I run the code, I get an alert box giving the confusing
message Date.getTime is not a constructor.

What is wrong here?

Error message: 'Date.getTime is not a constructor'


I have noticed that there is no error if I don't call the function
that contains the line above.

The following Stack Snippet reproduces the error.

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情话已封尘 2025-01-26 05:45:41

1。快速修复

只需在新日期之后立即添加括号

const currentTime = new Date().getTime();

2。说明

date


片段

new Date.getTime();

aha!我想我明白了你的意思:有一个班级
date.getTime ,现在您要调用默认构造函数
创建一个新对象: new Date.getTime(),对吗?

好吧,对不起,但是我找不到任何这样的构造函数。

,我可以听到您 - 程序员 - 尖叫否!那不是不是
我的意思! (愚蠢的✴!☠#@☠$%编译器!)

在您冷静下来以恢复合理正常的情况下
血压,您可能(耐心地)尝试以下操作:

编译器先生,我们显然脚下的脚 - 我从来没有
打算伤害你的感情。
我想要的是:请创建一个新的 date 对象,然后致电
该新创建的对象上的 getTime()方法。

看,让我按以下方式说,看看您是否更好地理解我的
视角?

const currentTime = (new Date).getTime();

丢失'()'调用构造函数new-parens

“调用默认构造函数,”()

编译器先生的回应:

Missing '()' invoking a constructor  new-parens

或 - 在更人性化的范围中。
好!因此,您要创建一个新的 date 对象?
,在该对象上,您要 .getTime()
很好。 - 除了您忘了告诉我哪个
您要我使用的构造函数。是默认构造函数吗?
或其他一些构造函数?
既然你没有告诉我,我会假设你
要我使用默认构造函数。

,但是下次问我时,您会考虑更明确吗
这样做吗?

这样,我就不必对您的意思做出有根据的猜测。

叹了口气后,您 - 程序员 - 程序员 - 再试一次:

const currentTime = (new Date()).getTime();

最后 - 编译器先生没有投诉!

这次,编译器先生沉默地回应,这很好
消息!
- 沉默的回应表明,编译器先生处于最佳状态
情绪。

不过,您是人类,而不是编译器,所以您可以想象
编译器先生说:
好的。因此(new Date()).getTime()表示您要创建一个新的
日期使用默认构造函数对象,然后调用
getTime()在该对象上。一切都很好。我没有抱怨。

但是由于您是一个非常敏感的人,所以您无法自助
问:

好!我很高兴我们终于同意这一点。但是有什么吗
还在你的想法吗? 。

请原谅我的

问 您的代码很有意义。没关系。确实是。
但是,既然,既然您一直在na缩 - 写(new Date())很好。
没有错。但是你不需要周围环境
括号。一旦您编写 new Date(),我就知道您
想要创建一个日期对象,即使您然后在此调用方法
对象。

不是愚蠢你知道!

-
比编译器错误更难理解和调试
警告。
这是堆栈溢出问答可能会有所帮助的时候。
正如评论中指出的那样,一个较短的替代方案
new Date()。getTime()是普通而简单 date.now()

这是没有错误的堆栈片段。

function rightNow() {
  const currentTime = Date.now();
  return currentTime;
}
console.log(rightNow().toString());
.as-console-wrapper { max-height: 100% !important; top: 0; }

参考

1. Quick fix

Just add parentheses immediately after new Date :

const currentTime = new Date().getTime();

2. Explanation

Date.getTime is not a constructor

If the compiler were a thinking human being, it could respond to the
snippet

new Date.getTime();

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 constructor
to 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 call
the getTime() method on that newly created object.

Look, let me put it as follows, to see if you better understand my
point of view?

const currentTime = (new Date).getTime();

Missing '()' invoking a constructor new-parens

To invoke the default constructor, '()' is missing.

The response by Mr Compiler :

Missing '()' invoking a constructor  new-parens

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:

const currentTime = (new Date()).getTime();

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 new
Date object using the default constructor, and then call
getTime() 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 you
want 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 simple Date.now().

Here is a Stack Snippet without the error.

function rightNow() {
  const currentTime = Date.now();
  return currentTime;
}
console.log(rightNow().toString());
.as-console-wrapper { max-height: 100% !important; top: 0; }

Reference

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文