-
-
Notifications
You must be signed in to change notification settings - Fork 225
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Improper let scoping #153
Comments
Sounds like we don't want to combine variable declarations in the loop if the outer declaration is also a let/const and only if it's a var - or maybe only do it if not referenced outside? |
That would work nicely. You could take it a step further and remove any surrounding blocks if all the variables within it are moved into the for loop. For example: {
let x = 0;
for(let i = 0; i < 10; ++i) {
x++;
}
} Should become: for(let x=0,a=0;10>a;++a)x++; Notice we can safely remove the surrounding block. |
Note that there is a slight difference in those two if the loop body had an async callback in it since mutating x from inside the callback would not work the same way as when it is declared outside the loop. |
Good point. I guess it's safer to not do that kind of transformation at all. |
is converted to:
x is being moved into the first part of the for loop which causes it to be scoped only to the for block. The call to console.log(x) is outside of the block and thus throws a ReferrenceError.
The text was updated successfully, but these errors were encountered: