Slide 12
Slide 12 text
function makeSheepCounter( limit ){
var count = 0,
goal = limit || 2;
return function sheepCounter(){
count++;
return count > goal;
};
}
var areWeThereYet = makeSheepCounter();
var noReallyAreWeThereYet = makeSheepCounter( 1 );
areWeThereYet(); // false
areWeThereYet(); // false
areWeThereYet(); // true
noReallyAreWeThereYet(); // false
noReallyAreWeThereYet(); // true
lørdag den 26. maj 12
Each returned function has it’s own context, and in this it’s own count and limit
So, to reiterate, a closure is a function that provides context and returns a new function