⟩ Give an example of closure?
Following example shows how the variable counter is visible within the create, increment, and print functions, but not outside of them −
function create() {
var counter = 0;
return {
increment: function() {
counter++;
},
print: function() {
console.log(counter);
}
}
}
var c = create();
c.increment();
c.print(); // ==> 1