G
G
gleendo2015-02-03 19:03:59
JavaScript
gleendo, 2015-02-03 19:03:59

How to understand moment in closure(JS)?

There is an example in the textbook. I can't understand why the solution works only if the makeCounter function is accessed via the counter variable ( var counter = makeCounter(); ).
If you don't create a variable and write console.log( makeCounter() () ); then nothing works.
Why put a function in a variable?

function makeCounter() {
  var currentCount = 0;

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

var counter = makeCounter();

console.log( counter() );
console.log( counter() );
console.log( counter() );
console.log( counter() );
console.log( counter() );
console.log( counter() );
console.log( counter() );
console.log( counter() );

Answer the question

In order to leave comments, you need to log in

5 answer(s)
M
Maxim Dunayevsky, 2015-02-03
@evgeniy8705

I rewrote the code, I hope it became more clear:

function makeCounter(){
    //Эта переменная видна только здесь
    var currentCount = 0;
    
    //Эта функция - тоже только здесь, зато она видит currentCount
    function getCountValue(){
        currentCount += 1;
        return currentCount;
    }
    
    //Возвращаем саму функцию, а не её значение
    return getCountValue;
}

//counter = getCountValue
var counter = makeCounter();

counter(); //На самом деле - вызов getCountValue()

V
vsvladimir, 2015-02-03
@vsvladimir

makeCounter() returns a counter function that will produce 1 more value each time it is called. And makeCounter for each call to itself stores the current value of the counter in the currentCount variable.
You can make several independent counters:
var counter1 = makeCounter();
var counter2 = makeCounter();
makeCounter()() - will create a counter and run it 1 time (result 1).
If you run makeCounter()() again, a new counter will appear (result 1).
The counter variable is needed to remember the same counter, and not create a new one every time.

S
Sergey Semenko, 2015-02-03
@abler98

In the example, first the makeCounter function is assigned to a variable (even the name of the function says this), which in turn returns another function, as a result, the variable counter = function() {
currentCount++;
return currentCount;
}; which we then call

W
WhoMe, 2015-02-03
@WhoMe

Already figured out?
Everything is working.
The call to makeCounter() creates a new currentCount each time and returns a new function.

console.log( makeCounter() () );
console.log( makeCounter() () );

It will be roughly equivalent
var counter1 = makeCounter();
var counter2 = makeCounter();
console.log(  counter1() );
console.log( counter2() );

A
Alexey Ukolov, 2017-09-09
@esvils

Assignment of objects in js goes by reference, so after execution item_all = item;both item_all and item are the same object and any changes will be reflected both there and there.
javascript clone object

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question