Why when you run the function circuit, the variable inside is not overwritten?

Here is the code circuit, the point I realized - the Function creates the object which will contain all the local variables. And any functions that we define inside will have a reference to this object. If not (nested functions) then it makes no sense to store these links.

function makeCounter() {
 var count = 1;

 return function() {
 return count++;
};
}

var counter = makeCounter();

// each call to increments a counter and returns the result
alert( counter() ); // 1
alert( counter() ); // 2
alert( counter() ); // 3


Question - why does each call to counter object reference not Peresopnytsia occurs when initializing the count variable (var count = )?
March 23rd 20 at 19:27
3 answers
March 23rd 20 at 19:29
Solution
F-tsiya makeCounter is invoked once. Accordingly, ONCE declared and initialized with a unit count variable . This variable is part of the scope created when you call makeCounter. This function returns another function (let's call it f for convenience)

return function() {
return count++;
};


which also has access to this scope which was defined count variable. That's all. Then you call this function (f) many times and each time it runs with the same variable count.

Try to do this :

var counter = makeCounter(); // assigned a function which is its OWN scope 
var counter2 = makeCounter(); // assigned a function which is its OWN different from the previous scope


there will be two scopes and each one will have its own separate count.
try to polizyvat

console.log( counter());
console.log( counter2());
March 23rd 20 at 19:31
Solution
Because the function call makeCounter stored in the counter variable, which is then repeatedly invoked.
Overwriting the count variable was when the function was called so
makeCounter()()
makeCounter()()
makeCounter()()
In the variable named counter to store the function result or something? - Werner_Waters commented on March 23rd 20 at 19:34
@Werner_Waters, Yes, makeCounter returns a function, this function and written to the counter
return function() {
 return count++;
};
- ayana_Beer commented on March 23rd 20 at 19:37
@Werner_Waters, the variable counter is stored Function.
After a call f-II makeCOunter returns this: return function(){..}
Can be returned from a function not only number, string, array. But another function.
Which, incidentally, remembers her surroundings. The variable count in this case. - Clint15 commented on March 23rd 20 at 19:40
@Adrianna53, "at every call" which of the TWO functions? ; ) - Clint15 commented on March 23rd 20 at 19:43
@Adrianna53, the interpreter will not pass this place every time.
makeCounter() worked once, recorded in the count unit and returned to another function (and that remembered their surroundings).

Each call to counter() is executed only the functioncounter() and nothing more. String var count = 1; was out and no more in the business. - Clint15 commented on March 23rd 20 at 19:46
@Clint15, all seem to understand these circuits. Damn, it's so simple, the function referred to by the returned function, and the variable just became this function that is returned. Thanks you have no idea how long I tried quite get the trick. Really don't know yet where this can be useful, but it is still very annoyed that I could not understand - Adrianna53 commented on March 23rd 20 at 19:49
March 23rd 20 at 19:33
Because you stored it in a variable "counter" after it is thrown, the counter is also reset.

And calling it as a method simply call roughly speaking the function inside it.

Find more questions by tags JavaScript