[英]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,
根据纯函数的定义,
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.