I have a Javascript main file that is enclosed by an immediately-called closure (so as not to pollute 'global':
(function () {
"use strict";
var closureVariable = [];
...
}());
I made a simple, bone-headed coding error when removing a variable from a function header such that my code had a comma instead of a semicolon:
function fred () {
var i,
closureVariable = [1,2,3,4];
confused();
}
function confused () {
console.log(closureVariable); // Prints '[]'
}
Certainly the missing semicolon on the 'var i' line was the problem. However the behavior I thought should happen is that my now locally-defined variable 'closureVariable' should have shadowed the higher-level scope definition, and the value of my locally-defined variable should have been available to functions lower down in the scope chain (that is, function 'confused' should have printed out '[1,2,3,4]';
What am I not understanding about Javascript scope chains here?