在C语言中,您可以通过switch case限定变量的作用域,就像这样。
使用以下代码时,在javascript中我会收到意外的标记:
const i = 1
switch (i) {
// variables scoped to switch
var s
var x = 2342
case 0:
s = 1 + x
break
case 1:
s = 'b'
break
}
编辑:
这是我考虑过的一个解决方法,但最终没有起作用。原因是每个case都有自己的作用域。
const i = 1
switch (i) {
case i:
// variables scoped to switch
var s
var x = 2342
case 0:
s = 1 + x
break
case 1:
s = 'b'
break
}
JS
中的switch
语句无效。你可以在case
内或switch
外定义变量。 - Ionut Neculaswitch
之后访问s
,为什么要将其限定在switch
语句内部呢?! - deceze{ code }
就像ABC一样简单。 - Phani Rithvij