简体   繁体   中英

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. It does not matter at all how that constant did get its value (in this case, during the compilation of the module).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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