简体   繁体   English

是否在Node.js中的函数内访问__dirname被视为不纯代码?

[英]Is accessing __dirname within function in Node.js considered as impure code?

If I have a function: 如果我有一个功能:

function testDirectory(filePath) {
    return __dirname === path.dirname(filePath);
}

Can I call this a pure function? 我可以称其为纯函数吗? By definition of pure function, 根据纯函数的定义,

  1. It doesn't cause any observable side effect. 它不会引起任何明显的副作用。
  2. It always evaluates the same result for same input value. 对于相同的输入值,它始终会评估相同的结果。
  3. Both the criteria mentioned above are satisfied. 满足上述两个条件。

However, depending on where this function is defined, the result changes. 但是,根据定义此功能的位置,结果会改变。 So, in a way it is dependent on context ( location in this case.) 因此,在某种程度上,它取决于上下文(在这种情况下为位置 )。

Yes, this function is pure since __dirname is a constant. 是的,此函数是纯函数,因为__dirname是常量。 It does not matter at all how that constant did get its value (in this case, during the compilation of the module). 该常量如何获得其值完全无关紧要(在这种情况下,是在模块编译期间)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM