0

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?

1 Answers1

0

In Javascript, every function is a closure.

Every function in javascript has access to lexical scope (the scope in which it is created).

Arrow functions, don't have specific functional scope. But, they still have access to the parent scope.

Every function's access to parent scope is through closure. If a function does not have parent function, it will have access to global scope due to closure.