简体   繁体   English

新的Date()何时在我的函数中运行?

[英]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()

  • Is it calculated every time the function isCurrentDateInRange called? 它是在每次调用isCurrentDateInRange函数时计算的吗?
  • Or is it calculated once when loading the file for the first time? 还是在第一次加载文件时计算一次?

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.

相关问题 当我运行我的脚本时,它输出 mml2tex is not a function - When I run my script, it outputs mml2tex is not a function 当我退出我的 Node 应用程序时,我可以运行 function 吗? - Can I run a function when I quit my Node App? TypeError: puppeteer.use(...) 在尝试运行我的代码时不是 function - TypeError: puppeteer.use(...) is not a function when trying to run my code 新的Date()函数提供了不需要的日期字符串 - new Date() function is giving an unwanted Date String 当我运行“npm install”命令时,没有安装任何东西,我在终端中得到一个空的新行 - When I run "npm install" commands nothing installs and I get an empty new line in my terminal javascript-在给定日期的午夜运行函数 - javascript - run a function at midnight of a given date 尝试运行我的应用程序时出现相同的错误。 “TypeError:this.microservicesModule.register 不是函数” - Getting the same error when trying to run my application. “TypeError: this.microservicesModule.register is not a function” 我正在尝试在我的服务器上运行我的新反应项目 - I'm trying to run my new react project on my server 为什么我的回叫功能无法运行? - Why does my call back function not run? 我在节点后端运行 npm run dev 时出现问题 - Problem when i run npm run dev in my node backend
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM