3

I have the following two counter functions which return different result when they run.

In the first snippet, counter function is assigned to variable letsCount and it looks like executing the function updates the var count in the counter function.

However in the second snippet, executing the function directly doesn't update the count variable.

Would you please explain why they have different results and what happens when the function that returns function is assigned to a variable?

Snippet 1

function counter() {
    var count = 0;
    return function() {
        console.log(count++);
    }
}
var letsCount = counter();
letsCount();    // 0
letsCount();    // 1
letsCount();    // 2

Snippet 2

function counter() {
    var count = 0;
    return function() {
        console.log(count++);
    }
}
counter()();  // 0
counter()();  // 0
counter()();  // 0
1
  • 3
    Whenever you call counter, you create a new function and set count to 0. Commented Nov 16, 2012 at 0:11

4 Answers 4

2

Every time you call counter() you create a new anonymous function instance, with its own scoped variables. If you want to keep using the same function, you will have to do something like this :

var counter = (function () {
    var count = 0;

    var fn = function() {
        console.log(count++);
    };

    return function () {
        return fn;
    };
})();

counter()();  // 0
counter()();  // 1
counter()();  // 2

A single anonymous function will be created then stored in a scoped fn function, then we return a function which, when called, will returns the value kept by fn.

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

Comments

1

Snippet 1 and Snippet 2 differ in their invocation. Your first snippet has a reference to the one returned function, and that function holds onto its scope (is a closure, has a reference to count).

Your second snippet calls the outer function each time, always returning a reference to a new function, with a new closure to a fresh count.

Comments

0

In the first case you are referencing it with a function pointer.. So the context is saved

Whereas in the second case you are calling the function , wherin the count is 0 . So the variable is out of context in here,, So you see the value as 0

Comments

0

It actually makes perfect sense why you're getting that behavior. When you call counter()(), the first counter() call is executed, effectively resetting the variable count to 0. When you set a variable to counter(), you are actually setting it to the returned function:

var letsCount = // (function() {
    // var count = 0;
    return function() {
        console.log(count++);
    }

// })();

Then when you call letsCount, you're calling the returned function and not the outer function.

Comments

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.