1

I am tasked to trace the cause of a memory leak in one of our applications so I'm trying to study closures. I wonder if this code creates closure:

function foo(p)
{
    return function(){ return p + 1; }
}

Based on my understanding, closure is created when the inner function gains access to a local variable of its parent function. The parameter p is local to foo, if the inner function gains an access to p, does it mean a closure is created?

4
  • FYI, every function in JavaScript is a closure, because every function has access to higher scopes. Commented Jul 11, 2013 at 9:26
  • @FelixKling you mean to say that every function has a snapshot of higher scope? Commented Jul 11, 2013 at 9:29
  • I wouldn't say snapshot, because it sounds like the function has access to a copy of all higher/free variables. It's rather that the environments are linked. If a variable is not found in the function's own environment, it is looked up in the "parent" environment, etc. So yes, if the function actually has a free variable, it can lead to a memory leak. If it doesn't, it potentially still has access, but I would assume that engines are smart today and will still garbage collect the values. Commented Jul 11, 2013 at 9:32
  • Although it is true that every function has access to higher (outer) scopes, not everyone agrees that all functions (more accurately their execution contexts) are closures. Many people, including myself, maintain that (in javascript) a true closure exists only when a persistent reference to an inner function is maintained after the outer function has completed and returned. Prior to the outer function's return, Garbage Collection will not delete inner variables regardless of whether an inner function exists or not. It is generally unhelpful when learning to regard all functions as closures. Commented Jul 12, 2013 at 0:40

3 Answers 3

1

The parameters of a function exists in the local scope of the function, so yes it creates a closure

Sign up to request clarification or add additional context in comments.

Comments

1

Yes, that is exactly what's happening here. The inner function you're returning has access to the parameter p through the local scope, so you're correct.

It would also have created a closure if you referenced a local variable from the outer function in the returning function, like that:

function foo(p) {
    var q = 4;
    return function() { return p + q; }
}

Here's a very detailed explanation: Explaining JavaScript Scope And Closures

Comments

0

This does create a closure, but it is not the typical closure that is talke about in javascript. What is the typical example is:

var adder = function(a) {
  return function(b) {
    return a + b;
  }
}

What this does is give you the ability to create a "closure" or close in a variable to be used over and over again. I can create a function:

var adder4 = adder(4);

Now if I want to add 4 to any number I can user adder4(2) and the result in this case would be 6. What is going on here is that the 4 is inserted for the variable a. The variable a is then enclosed inside this function permanently. We can then replace the variable b at any time to create a new function. So when I made the function call adder4(2), I am using a function in which a has already been assigned. The variable 2 in that case is assigned to the variable b. Obviously when you add 4 and 2 you get 6. But you can use the same function to add another number, adder4(3). Now this does the same logic and gives you 7. The 4 is still enclosed, or in the "closure", but you are free to replace another variable in the middle of the function.

You also see this a lot with anonymous functions and click handlers, but you can google that for much better answers.

Hope this helps.

3 Comments

A closure is simply an environment,function pair. What the function is actually doing does not matter.
Could you please explain what you mean by environment, function pair? I wrote this to try to understand closures better and would love some clarification.
I think Wikipedia explains it quite well: en.wikipedia.org/wiki/Closure_%28computer_science%29.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.