do while scope
Is there a language where lexically scoped variables declared in a block are accessible outside of that block? Java, C, C#, rust, Python, etc, will not let you do this. I’m not sure this is a gotcha for most software developers
er, to add to that, “a” shouldn’t be undefined during the while condition, this should throw — it’s a reference error
Not sure about others, but i assume the condition part and statement part in a control construct live in the same scope.
That's why you can do for (let a = 1; a < 2; a++) a;
. And if (let a = 1) a;
maybe in es7?
Why that's not the case for "do while"? Is my mental model wrong?
Well, there's a lot of precedent I'm languages for it (the while condition) to not be a part of the block scope. So from that prospective it's unsurprising, though it may surprise developers accustomed to variable hosting
Ah, yes, I meant reference error.
I just realized maybe my mental model is wrong:
Is it correct that this is the scope chain for a "for" loop?
top scope for condition scope for statement scope
Statement can reference variables defined in condition, but not the other way around?
On 17 April 2015 at 17:18, Glen Huang <curvedmark at gmail.com> wrote:
Not sure about others, but i assume the condition part and statement part in a control construct live in the same scope.
That's why you can do
for (let a = 1; a < 2; a++) a;
. And `if (let a =
- a;` maybe in es7?
Why that's not the case for "do while"? Is my mental model wrong?
Yes. :)
for (let x ...) scopes over the body naturally. In your example, you expect to scope an inner declaration (one even inside a block) to scope to the outside. That is something else entirely, and makes no sense to me.
Thank you.
I just post my new understanding a few seconds earlier. Hope that's right. :)
do { let a = 1; } while (a);
is "a" undefined in the while condition? This seems like a gotcha.