When you redefined closVariable, omitting the semicolon, it was redefined only in the context of the fred function. The confused function exists in the context of your closure, so it still sees the original closure. If you had a confusing function defined inside the fred function, it saw a new Variable closure and printed [1,2,3,4]
(function () {
"use strict";
var closureVariable = [];
function fred () {
var i,
closureVariable = [1,2,3,4];
function confused () {
console.log(closureVariable);
}
confused();
}
})();
Or if you want to cause confusion from abroad fred ()
(function () {
"use strict";
var closureVariable = [];
var confused;
function fred () {
var i,
closureVariable = [1,2,3,4];
confused = function () {
console.log(closureVariable);
}
}
confused();
})();
source
share