As others have mentioned, you've basically created two closures.
The easiest way to understand closures is that it is a generalisation of the concept of global variables. In fact, global variables in javascript is nothing more than a closure in global scope.
A closure is the mechanism by which the body of a function may reference variables in outer scope. As I said above, global variables are nothing more than a closure:
var x;
function x_plus_plus () {
x++; // this variable is captured by a closure
}
Every scope allows you to create a closure. So apart from the global scope you can also create a closure inside other functions. To me, personally, the minimum example code that best illustrates what closures are is an IIFE that defines two functions that share a variable:
var incr;
var val;
(function(){
var x = 0; // shared with the functions below var via closure
incr = function(){x++};
val = function(){return x};
})();
incr();
incr();
console.log(val()); // outputs 2 because the functions share x
console.log(x); // syntax error - x does not exist in this scope
Note that a closure is what happens between a variable and functions declared in that variable's scope. It is not what happens between a variable and the function the variable is declare in:
function () { <─────┐
├── this is not a closure
var foo; <───────┘
}
function () {
var foo; <────────────────┐
├── this is a closure
function bar () { │
do_something(foo); <───┘
}
}
var x; <───────────────┐
├── this is also a closure but we
function y () { │ normally call it "global variable"
do_something(x); <───┘
}
Side note: Global variables in js are actually a bit unique because they're also properties of the global (window) object. But in theory they behave as if they're an instance of a closure. The point is that thinking of closures as a global-variable-like mechanism is the easiest way to understand them.