我在使用 NodeJS 时遇到了 2 个相互冲突的命名约定。有些变量以单下划线开头,例如 _temp
,而有些变量以双下划线开头,例如 __dirname
。
我一直在尝试寻找这种命名约定差异的答案。我查看了Double Underscore in front of a variable但答案非常具体于 __dirname
和 __filename
。它没有解决冲突的命名约定。
如果有人能为我提供这方面的资源,那就太好了。
最佳答案
即使我是 Node.js 新手并正在寻找答案。在node.js文档中找到了答案。请参阅Node.js - Globals了解更多详情。
正如上面的答案,带有单下划线(_private)的变量是定义私有(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.
关于javascript - NodeJS 中的双下划线与单下划线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35946339/