实际上你可以在 switch 内部循环中使用它,在这种情况下 break/continue 是一个有用的组合
看看这个例子
var array = [1, 2, "I am a string", function(){}, {}, 1,2,3]
for(let i in array){
switch(typeof array[i]) {
case "number":
array[i] += " I was a number"
break; // break from the switch but go on in the for-loop
// ie. go to *put esclamation mark*
case "function":
array[i] = "function"
// fall through
default:
array[i] = `this is a ${JSON.stringify(array[i])} object`
break; // also here goes to *put esclamation mark*
case "string":
continue; // continue with the next iteration of the loop
// ie. do not put exclamation mark in this case
}
array[i] += "!" // *put esclamation mark*
}
/*
array will become
[ "1 I was a number!", "2 I was a number!", "I am a string", "this is a \"function\" object!", "this is a {} object!", "1 I was a number!", "2 I was a number!", "3 I was a number!" ]
*/