Understanding the concept of closure in deep

Hey check this code here,

function getspinner(){  
    var count = 0;  
    function increament(){  
        return ++count;  
    }  
    function decreament(){  
        return --count;  
    }
    return {  
        up:increament,  
        down:decreament  
    }  
}

      

If I call him

var spinner = getspinner();  
spinner.up(); // value increments by 1 each time  
spinner.down(); // value decrements by 1 each time

      

But when I call it Like

getspinner().up(); // i'l get 1 each time  
getspinner().down(); // i'l get -1 each time

      

I would like to know the difference. why doesn't the lifetime of the account increase in the case of getspinner (). up () or getspinner (). down ();

+3
javascript closures


source to share


No one has answered this question yet

Check out similar questions:

7649
How does JavaScript blocking work?
5101
What's the most efficient way to deeply clone an object in JavaScript?
2660
Closing JavaScript Inner Loops - A Simple Practical Example
1001
Is Safari iOS 6 caching $ .ajax results?
743
What is the difference between "close" and "lambda"?
724
Change selected dropdown value with jQuery
567
Why would we use async thread middleware in Redux?
513
JavaScript closure versus anonymous functions
325
What is the difference between (for ... in) and (for ... out) operators in JavaScript?
1
Understanding JavaScript locks - freeze variable passed for callback



All Articles
Loading...
X
Show
Funny
Dev
Pics