3 ms·
One more and then I'll shut up :) It looks like my previous example was affected by eval. The following reports foo's value as undefined while breaking for th
by schristo 15y ago
One more and then I'll shut up :)
It looks like my previous example was affected by eval.
The following reports foo's value as undefined while breaking for the debugger statement in Firefox and Chrome:
(function () {
var foo = "bar";
return function() {
debugger;
};
})()();
While the following reports foo's value as "bar" while breaking for the debugger statement:
(function () {
var foo = "bar";
return function(_var) {
debugger;
return eval(_var);
};
})()('foo');
- bzbarsky 15y agoThe first example there allows the JS engine to prove to itself that no one uses foo, so it optimizes the reference out of the closure to save memory. That's an optimization (and the fact that the debugger doesn't disable it is actually a bug); the language semantic is that all closed-over variables are available to the closure function body.