Closed bl4de closed 9 years ago
IIUC the only explanation for this is that in whatever environment you're trying this in, there's already an a
in the outer scope, diff from the a
in the loop. Or whatever implementation you're running in has a massive bug. a
most definitely should not be accessible in that scope.
This is totally insane, but you're correct as usual.
I've tried code above in my Chrome Dev Tools (created some small snippet for this). Yeah, and there was an a in Chrome Dev Tools console defined an hour or two earlier today :/
In fresh environment, the code collapsed right after first call on fn(), which was of course expected.
I am sorry for this issue, I should check this before I've created one. Shame on me.
No worries. :) Happens all the time, actually.
Hi,
I've got a question about scope created by let.
We've got an example (similar as in YDKJS):
Works perfect, closure in setTimeout() has access to valid a in each iteration as is defined inside scope created by let.
But let's do something like this:
I define fn() outside of scope created by let (in for statement). Also there's one more block scope created by outside brackets. a should not be visible, right? And everything is enclosed in foo() which is created yet another scope.
And the result is:
fn() has an access to a as is called inside the scope, where a is defined (right?). But how a might be accessible outside of all scopes which surround it? (this line with console.log(a), where there's undefined - as I was expecting Reference Error, the same as for b)
And why a in each iteration, printed by fn(), behaves as if fn() was declared as closure, similar as in your example from "Loops + Closure" chapter?
Is there's something I didn't I understand from scopes?