Click on the link below.
x and y are in the local scope. a is in the scope in which the closure was declared. g is in the sub scope of that. w is in the scope of the call to closureTest(). This test shows that a JavaScript anonymous function takes a references to the entire scope in which it was declared, not just the objects it explicitly references, Which implies that all anonymous functions are closures, which makes avoiding closures rather tricky.
call closureTest()
window.onload = init;
var closureTest = null;
var g = 17;
function init()
{
var a = 42;
closureTest = function()
{
var x = 1;
var y = 2;
// Use Eval to work out what we can see from the scope
alert("x = " + eval("x"));
alert("y = " + eval("y"));
alert("a = " + eval("a"));
alert("g = " + eval("g"));
}
}