const与var在node . js中“需要”模块时是否存在效率差异

时间:2022-08-21 23:55:03

I was reading the documentation for https://github.com/rvagg/bl and I noticed that, in the examples, they use const to require a module and this made me wonder: is this a good practice? I mean, to me, this looked as a good idea.

我正在阅读https://github.com/rvagg/bl的文档,我注意到,在示例中,他们使用const来要求一个模块,这让我想知道:这是一个好的实践吗?对我来说,这是个好主意。

A direct example from the link above is:

上面链接的一个直接例子是:

const BufferList = require('bl')

var bl = new BufferList()
bl.append(new Buffer('abcd'))
bl.append(new Buffer('efg'))
/*...*/

I also noticed the lack the semicolons in the example but well, that has been discussed elsewhere thoroughly.

我还注意到示例中缺少分号,但是其他地方已经详细讨论过了。

1 个解决方案

#1


37  

The const makes perfect sense here:

这里的const完全说得通:

  • It documents that the object reference is not going to change.
  • 它记录对象引用不会改变。
  • It has block scope (same as let) which also makes sense.
  • 它具有块范围(与let相同),这也有意义。

Other than that it comes down to personal preference (using var, let or const)

除此之外,它还取决于个人偏好(使用var, let或const)

#1


37  

The const makes perfect sense here:

这里的const完全说得通:

  • It documents that the object reference is not going to change.
  • 它记录对象引用不会改变。
  • It has block scope (same as let) which also makes sense.
  • 它具有块范围(与let相同),这也有意义。

Other than that it comes down to personal preference (using var, let or const)

除此之外,它还取决于个人偏好(使用var, let或const)