Skip to content Skip to sidebar Skip to footer

JavaScript - How Do I Learn About "closures" Usage?

From Wikipedia, the free encyclopedia: Closure (computer science) In computer science, a closure is a function that is evaluated in an environment containing one or more bou

Solution 1:

Searching for "javascript closures" gave plenty of encouraging-looking links. The top three were these two (the third link was a reformatted version of the second):

If these didn't help you, please explain why so we're in a better position to actually help. If you didn't search before asking the question, well - please do so next time :)


Solution 2:

A practical example of closures is when they are used to create "Private" variables, like

function incrementer(){
    var i=0;
    this.increment=function(){
        i++;
    }
    this.get=function(){
        return i;
    }
}

The only way to access i is to use the method get, and the only way to change it is to use the method increment. In classes, you can do this to emulate private variables.


Post a Comment for "JavaScript - How Do I Learn About "closures" Usage?"