javascript - 将库声明为常量

标签 javascript node.js variables constants

在 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/

相关文章:

javascript - Prestashop 1.7 管理员中的谷歌分析

javascript - 如何生成等量的不同字符串

javascript - 为什么我的网页转换不起作用? (HTML、CSS、JavaScript)

jquery - Node.js 中未定义 JSONP,使用 .send 会导致意外的 token 错误

php - 变量在 PHP joomla 的 where 子句中不起作用

javascript - Bootstrap 模态自动在第二个打开模态时添加右边距

javascript - 对于所有包,npm install 总是返回 `code ECONNREFUSED` 错误

node.js - 使用 npm 版本补丁时自定义提交消息

PHP - 尽管打印文本,但为什么不打印变量?

python - 使用 pandas 导出 csv 时指定第一列