nodeJS中的双下划线与单下划线

Double underscore vs single underscore in nodeJS

我在使用 nodeJS 时遇到了 2 个冲突的命名约定。有些变量以单下划线开头,例如 _temp,而有些变量以双下划线开头,例如 __dirname.

我一直在努力寻找这个命名约定差异的答案。我查看了 Double Underscore in front of a variable,但答案非常针对 __dirname__filename。它没有解决冲突的命名约定。

如果有人能为此提供资源,那就太好了。

我可能是错的,但据我所知,js 中只有一个约定:"if method or variable supposed to be private, use underscore in front of it - _privateMethod"。甚至这个也是 "unofficial"。双下划线不是命名约定。只是来自 node 的一些开发人员决定这样命名。

即使我是 node.js 的新手,也在寻找答案。在 node.js 文档中找到了答案。请参阅 Node.js - Globals 了解更多详情。

如上回答所述,单下划线变量(_private)是定义私有变量。

双下划线 (__) 在 node.js 中没有任何约定。 node.js中只有两个带双下划线的变量(称为全局对象)。

__dirname : used when to get the name of the directory that the currently executing script resides in.

__filename : used to get the filename of the code being executed.