-
Notifications
You must be signed in to change notification settings - Fork 33.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Scopes & Closures Ch. 6 - Switch Statement Scoping #1670
Comments
The cited quote is asserting that the outer pair of If there are no IOW, this is not a block-scope (and also not syntactically allowed): switch (true) {
// not block scoped to the switch (but also syntactically disallowed)
let foo = 42;
console.log(foo);
} But this is a block-scope: switch (true) {
case true:
// the scope here is the `case` clause, specifically its statement list, even
// without any explicit { }
let foo = 42;
console.log(foo);
} |
If case statements implicitly create a scope, I'd expect this to produce a reference error (via fallthrough): switch (true) {
case true:
let foo = 42;
case 'anyvalue':
console.log(foo);
} Unexpectedly however, it ouputs |
Welp... I'm stumped. That's not what I understood. |
According to the spec, the |
Ok. Appreciate you taking the time to look into this. Thanks again for all your work on the book and for making it available online! If you ever have time to join us again some time let us know! |
@getify this project seems like fun. I want to contribute. How do i go about that? The documentation is not quite self sufficient |
@karenefereyan This isn't really a "project" per se. It's a series of books I wrote (and am writing). Contributors are primarily those who find typos that my copyeditor may have missed, or occasionally find technical mistakes (such as this thread). As for contributing in that capacity, see these guidelines. |
Switch is a structural directive which natively defines how it handles execution - matched cases should execute. The case items themselves should be seen as they are all in one scope. This means each case acts like a piece of code that will execute when the it case matches the switch clause, all cases are in a transient scope under the switch directive, so ideally the switch can be called a block and not a scope. |
This isn't entirely true, since if you put |
Okay thanks |
I understand it is scope when you put {...} around the statements. But the whole list of cases acts as if they are in one scope inside the switch when the case bodies are one-liner expressions as commented by @ericmathison
Outputs But when we put them in curly braces, scoping rules is effected:
Throws an error |
Please type "I already searched for this issue":
I already searched for this issue.
Edition: (1st or 2nd)
2nd
Book Title:
Scope & Closures
Chapter:
Chapter 6: Limiting Scope Exposure
Section Title:
Scoping with Blocks
Problem:
As best I can tell, the switch statement curly-braces referred to do create a scope with
const
orlet
.For example, with
var
, foo outputs'foo'
as expected.However, with
let
, loggingfoo
produces a reference error. (Not expected if these braces aren't creating a scope)This came up in our last online Code Club discussion (which I believe you have visited previously). Thanks so much for providing this resource!
The text was updated successfully, but these errors were encountered: