I've learned that a pure function is a function that doesn't alter global state, period. If this is true, functions within functions can alter the state of the outer function and still be pure, correct?
Example:
function func1() {
let name = "My Name"
func2()
function func2() {
// alter name here.
}
}
In the above example, func2
is still pure because it doesn't use any global state.
That's how I see, but my working colleagues believe that func2
is not pure, and it should be writen like:
function func1() {
let name = "My Name"
func2(name)
function func2(name) {
// use name here.
}
}
Which is bad, because:
- if the v8 doesn't optimize this, the CPU will run more instructions
- shadowing is a bad practice
The question is: What exactly is a pure function when we are talking about a function within a function?