5

I have been reading a fair bit about closures in JS. I have been through various guides as this one https://medium.freecodecamp.org/javascript-closures-simplified-d0d23fa06ba4

I still have one question though. Does a closure only refere to first order function (function returning a function). Or, is any function a closure ? The only difference I see really is like with some function not nested, one of the 3 scope chain (outer function's scope) would be empty still it doesn't exist.

Scipion
  • 11,449
  • 19
  • 74
  • 139
  • We can say where ever we use the reference of the function to use the that function scope, we are using closure there.And to use the reference we either return object or another function from parent function. – ganesh phirke Apr 04 '19 at 14:11

2 Answers2

3

A closure is created by calling a function; the function itself isn't the closure. Conceptually, every function call implicitly causes a new closure to come into existence. For some functions, the closure is ephemeral and just vanishes as soon as the function returns:

function add2(n) {
  return n + 2;
}

That function returns only a number; nothing can refer to anything in the closure created by the function call, so the closure goes away and all you have left is the return value.

The closure becomes interesting when a function returns something that has one or more "hooks" into the local environment created when the function was called. (The function can expose the closure by modifying the global environment too.) So this function:

function addn(addend) {
  return function(n) {
    return n + addend;
  }
}

exposes the closure because the returned function has a reference to the parameter of the outer function.

I can't think of a way an ordinary function can expose a closure that doesn't somehow involve one or more functions that reference stuff from the local context (parameters, variables). (Generator functions are interesting, because yield kind-of always returns something that exposes the closure, I suppose.)

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • Note that in the first paragraph I wrote "Conceptually" — the runtime is free to optimize things if it figures out the closure can't be exposed. – Pointy Apr 04 '19 at 14:02
  • And in the last paragraph, what I'm thinking about is a function that returns an object (or object structure) that involves just *references* to local variables etc; I can't think of a reason that that would require the closure itself to remain "alive". – Pointy Apr 04 '19 at 14:05
1

A closure is the combination of a function and the lexical environment within which that function was declared

for example,

 function greet(Say){
   return (
          function(name){
             console.log(Say + ' ' + name);
          }
   )
}

Now I'm I can do something like this,

greet('Hi')('Alex')

This line will return the string 'Hi Alex'

Now I'm going to do something like this,

var sayGreet = greet('Hi');

If I console.log(sayGreet) it will be a function.

Now tell me where is this sayGreet variable defined? It is on the global level. Or in the other word global execution context.

Now let's do,

sayGreet('Alex')

inside this sayGreet function, we have a console.log(Say + " " + name) . We know name is Alex but what about Say? There is no sign of Say at this moment because the greet function already completed execution and returned a function.

Even though javascript still have the ability to refer to the Say value. Even it is completed the execution. This Say value can only be access by inner functions or nested functions.

This is call closure. A single function cannot be closure. There should be an outer lexical environment too.

Dilshan
  • 2,797
  • 1
  • 8
  • 26