JavaScript 切换奇怪的行为
JavaScript switch strange behavior
我有以下代码片段。
var caseObj = function () {
}
switch (typeof caseObj) {
case "function":
console.log("it is function");
case "object":
console.log("It is object now");
}
它的输出是
it is function.
It is object now.
但是 typeof caseObj
给出输出 function
但它仍然评估
例"object"例也
怎么可能?我做错了什么吗?
编辑:
typeof caseObj
给出 function
,所以它执行那个案例,但它也
执行 object
case.Why 这种奇怪的行为?
问题不在于 typeof
,但您错过了案例中的 break
语句。这将使 case
像 function OR object
并执行这两种情况的块。
您错过了 case
的 break;
声明。这就是接下来case
.
闹翻的原因
The break statement terminates the current loop, switch, or label statement and transfers program control to the statement following the terminated statement.
var caseObj = function() {
}
switch (typeof caseObj) {
case "function":
document.write("it is function");
break;
case "object":
document.write("It is object now");
break;
}
来自答案中的评论:
但如果没有匹配的大小写,它也会崩溃并退出 switch.But 它执行 case "object": statment as well.Why?
来自MDN
If a match is found, the program executes the associated statements. If multiple cases match the provided value, the first case that matches is selected, even if the cases are not equal to each other.
The optional break statement associated with each case label ensures that the program breaks out of switch once the matched statement is executed and continues execution at the statement following switch. If break is omitted, the program continues execution at the next statement in the switch statement.
switch
执行从第一个 case
匹配请求的值开始,从顶部开始。
然后,除非你在某处使用break
,否则它将继续执行从第一个匹配到底部的所有case
。
例如,如果您在示例中反转 2 case
,它只会输出 "it is function".
您没有在每个 case 子句结尾的 break 语句。当您这样做时,控制流将传递到下一个 case 子句的语句,而不管 case 条件是否满足。例如,如果您有:
var x = 1;
switch(x) {
case 0: console.log(0);
case 1: console.log(1);
case 2: console.log(2);
case 3: console.log(2);
}
输出将是:
1
2
3
但是如果你在每种情况下都设置中断
var x = 1;
switch(x) {
case 0: console.log(0);break;
case 1: console.log(1);break;
case 2: console.log(2);break;
case 3: console.log(2);break;
}
输出只是 1
我有以下代码片段。
var caseObj = function () {
}
switch (typeof caseObj) {
case "function":
console.log("it is function");
case "object":
console.log("It is object now");
}
它的输出是
it is function.
It is object now.
但是 typeof caseObj
给出输出 function
但它仍然评估
例"object"例也
怎么可能?我做错了什么吗?
编辑:
typeof caseObj
给出 function
,所以它执行那个案例,但它也
执行 object
case.Why 这种奇怪的行为?
问题不在于 typeof
,但您错过了案例中的 break
语句。这将使 case
像 function OR object
并执行这两种情况的块。
您错过了 case
的 break;
声明。这就是接下来case
.
The break statement terminates the current loop, switch, or label statement and transfers program control to the statement following the terminated statement.
var caseObj = function() {
}
switch (typeof caseObj) {
case "function":
document.write("it is function");
break;
case "object":
document.write("It is object now");
break;
}
来自答案中的评论:
但如果没有匹配的大小写,它也会崩溃并退出 switch.But 它执行 case "object": statment as well.Why?
来自MDN
If a match is found, the program executes the associated statements. If multiple cases match the provided value, the first case that matches is selected, even if the cases are not equal to each other.
The optional break statement associated with each case label ensures that the program breaks out of switch once the matched statement is executed and continues execution at the statement following switch. If break is omitted, the program continues execution at the next statement in the switch statement.
switch
执行从第一个 case
匹配请求的值开始,从顶部开始。
然后,除非你在某处使用break
,否则它将继续执行从第一个匹配到底部的所有case
。
例如,如果您在示例中反转 2 case
,它只会输出 "it is function".
您没有在每个 case 子句结尾的 break 语句。当您这样做时,控制流将传递到下一个 case 子句的语句,而不管 case 条件是否满足。例如,如果您有:
var x = 1;
switch(x) {
case 0: console.log(0);
case 1: console.log(1);
case 2: console.log(2);
case 3: console.log(2);
}
输出将是:
1
2
3
但是如果你在每种情况下都设置中断
var x = 1;
switch(x) {
case 0: console.log(0);break;
case 1: console.log(1);break;
case 2: console.log(2);break;
case 3: console.log(2);break;
}
输出只是 1