I know what closures are, they're functions that have access to their lexical scope, variables that would otherwise be unreachable.
Ex:
const curryMultiply = x => y => x * y;
Now, when calling curryMultiply
twice, we get access to the original parameter, nothing new to the average JS developer.
But, take this example instead:
const f = key => {
return window[key];
};
Now, it doesn't create a function at all, but, it has access to the global object, so in that example, does function f
have access to window
via closure?
How about this:
const TAU = Math.PI * 2;
export const mul_by_tau = x => x * TAU;
I know that in some languages, such as C++, these would just be functions, there's no extra thought put into it.
Previously, I had thought that global functions did not create closures, do they?