在 NodeJS 中“需要"一个模块时,const 和 var 之间是否存在效率差异 [英] Is there an efficiency difference between const vs var while 'requiring' a module in NodeJS

查看:20
本文介绍了在 NodeJS 中“需要"一个模块时,const 和 var 之间是否存在效率差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

推荐答案

const 在这里非常有意义:

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

除此之外,这取决于个人喜好(使用varletconst)

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

这篇关于在 NodeJS 中“需要"一个模块时,const 和 var 之间是否存在效率差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆