Let's take the following (JavaScript) code that returns a function that closes over variables x
and y
to illustrate:
function test() {
var x = Math.random();
var y = Math.random();
var f = function() {
console.log(x, y);
};
return f;
}
test()();
The code is nonsensical, I just want to close over x
and y
.
Variables x
and y
don't exist outside of the function test
but they remain available in the function f
.
Barring any compiler optimizations (like inlining), do we say there is a single closure (created by f
), or do we say there are two closures (for/over x
and y
)?
single
is the correct answer.f
.