Suppose I declare a variable called foo
in the ready function. Normally, locally declared variables are deleted when the function ends right?
I want to use this variable in the event listener of #someid
. I tried this and the foo
variable is still accessible when a click-event occurs on #someid
.
Why isn't this variable destroyed and still accessible, when the ready function ends? Is it safe to declare a variable and use it this way? I don't want to declare the variable globally, as I didn't.
EDIT: Where are these event listeners and their variables stored?
Here is my js:
$(document).ready(function() {
var foo = 0;
//random event listener
$('#someId').on('click', function() { foo++; }); //increment foo
});