在 NodeJS 世界中,我们需要使用 require
函数的模块:
var foo = require ("foo");
在 JavaScript 中(也在 NodeJS 中)我们有 const
关键字来创建常量:
const
Creates a constant that can be global or local to the function in which it is declared. Constants follow the same scope rules as variables.
例子:
$ node
> const a = 10
undefined
> a
10
> a = 7
7
> a
10
我的问题是:要求库作为常量会好吗?
例子:
const foo = require ("foo")
, http = require ("http")
;
/* do something with foo and http */
在需要库时使用 const
而不是 var
是否有任何坏/好的影响?
最佳答案
事实证明,使用 const
而不是 var
来获取依赖项已成为一种常见的做法。至少在 Node.js 源代码中是这样的:
所以,我想这是一个很好的做法。我也在my modules开始使用它.
关于javascript - 将库声明为常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23413963/