[英]When will new Date() be run in my function?
I'm still fairly new to Node and Javascript and just want to check what's going on here but I'm not sure what to even look for. 我对Node和Javascript还是很陌生,只想查看这里发生了什么,但是我不确定该寻找什么。
I've created a couple of functions for comparing dates... 我创建了一些用于比较日期的函数...
exports.isDateWithinRange = date => (from, to) => from <= date && date <= to;
exports.isCurrentDateInRange = isDateWithinRange(new Date());
These work (for now) but I just want to check. 这些工作(目前),但我只想检查一下。
In the second line... when is that new Date()
calculated? 在第二行中...什么时候计算
new Date()
?
isCurrentDateInRange
called? isCurrentDateInRange
函数时计算的吗? Because if it's the latter I need to change it. 因为如果是后者,我需要更改它。 But I didn't want to bother changing it if it's not a problem.
但是,如果这不是问题,我不想更改它。
Thanks 谢谢
The Date object is constructed whenever you require the module. 每当需要模块时,都会构造Date对象。 And the isCurrentDateInRange will be the result of the immediate invocation of
isDateWithinRange
. isCurrentDateInRange将是立即调用
isDateWithinRange
的结果。
To avoid it you can use the exported isDateWithinRange
and you can set default date value like this: 为了避免这种情况,您可以使用导出的
isDateWithinRange
并且可以设置默认日期值,如下所示:
exports.isDateWithinRange = (date = new Date()) => (from, to) => from <= date && date <= to;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.