Last active
January 16, 2017 03:55
-
-
Save LiuuY/e929dac85c55d491dfc6c6cf944ac568 to your computer and use it in GitHub Desktop.
Switch case scope
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://twitter.com/mxstbr/status/820228619549155328 | |
// https://babeljs.io/repl/#?babili=false&evaluate=false&lineWrap=false&presets=es2015&code=const%20someFunc%20%3D%20(something)%20%3D%3E%20%7B%0A%20%20switch%20(something)%20%7B%0A%20%20%20%20case%20'abc'%3A%0A%20%20%20%20%20%20const%20items%20%3D%20%5B'asdf'%5D%0A%20%20%20%20%20%20return%20items%0A%20%20%20%20case%20'xyz'%3A%0A%20%20%20%20%20%20const%20items%20%3D%20%5B'bla'%5D%0A%20%20%20%20%20%20%2F%2F%20%20%20%20%E2%86%91%0A%20%20%20%20%20%20%2F%2F%20%20%20%20Babel%20complains%20here%20about%20a%0A%20%20%20%20%20%20%2F%2F%20%20%20%20'Duplicate%20declaration%20%22items%22'%0A%20%20%20%20%20%20%2F%2F%20%20%20%20(see%20below)%0A%20%20%20%20%20%20%2F%2F%20%20%20%20Why%3F%3F%0A%20%20%20%20%20%20return%20items%0A%20%20%7D%0A%7D | |
const someFunc = (something) => { | |
switch (something) { | |
case 'abc': | |
const items = ['asdf'] | |
return items | |
case 'xyz': | |
const items = ['bla'] | |
// ↑ | |
// Babel complains here about a | |
// 'Duplicate declaration "items"' | |
// (see below) | |
// Why?? | |
return items | |
} | |
} | |
// solution: | |
// By creating another block scope for each case this is resolved. | |
// case 'abc': { | |
// const items | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment