javascript 未定义时刻 vs 空时刻

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33002861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 16:00:47  来源:igfitidea点击:

Moment of undefined vs moment of null

javascriptdatemomentjs

提问by rishat

If wonder why

如果想知道为什么

> moment(undefined).isBefore()
true

but

> moment(null).isBefore()
false

Is there any rational explanation for this behavior?

这种行为有什么合理的解释吗?

回答by Brian

moment(undefined)is equivalent to moment(), which assumes the initial state to be the current date/time.

moment(undefined)等价于moment(),它假定初始状态为当前日期/时间。

moment(null), on the other hand, is not a thing. It is not valid (at least not in the version I'm playing with), and has undocumented results.

moment(null),另一方面,不是一回事。它无效(至少不在我正在使用的版本中),并且具有未记录的结果。

Of course, you can read the source code, and find that isBeforedoesn't check for undefinedeither. In other words, momentjs is not expecting itself to be used this way, twice.

当然,您可以阅读源代码,并发现它们isBefore都没有检查undefined。换句话说,momentjs 并不期望自己以这种方式被使用两次。