Is there any way to access scoped variables from outside the scope?
For example how to edit count
from outside of this function:
var counter = (function(){
var count = 0;
return function () {
return ++count;
};
})();
Is there any way to access scoped variables from outside the scope?
For example how to edit count
from outside of this function:
var counter = (function(){
var count = 0;
return function () {
return ++count;
};
})();
It is not currently possible (nor do I think it will ever be possible) to access a local variable from outside the scope.
That is the whole idea.
What you can do is change the return of the function to return an object so that you can increase and decrease the count
variable:
function counterObject = (function(){
var count = 0;
return {
up: function(){ return ++count; },
down: function(){ return --count; }
};
})();
counterObject.up(); // 1
counterObject.up(); // 2
counterObject.up(); // 3
counterObject.down(); // 2
counterObject.up(); // 3
But aside from doing the above there is no real access to the local scoped variable.