[英]Unreachable code error when I use a break in my switch statement
I am wondering what I am doing wrong in my code. 我想知道我的代码中我做错了什么。 I am trying to do the following:
我正在尝试执行以下操作:
switch (action.type) {
case TYPES.ADD_TO_FAVORITES:
if (state.faves.length <= 10) {
return assign({}, state, {
faves: state.faves.concat(action.payload),
full: false
});
} else {
return assign({}, state, {
faves: state.faves,
full: true
});
}
default:
return state;
}
My linter says to add a break before default
case, but when I do that, it says unreachable code
. 我的linter说要在
default
情况下添加一个中断,但是当我这样做时,它会说unreachable code
。
A return
will act as a break
. return
将作为break
。 There is no need to use both. 没有必要同时使用两者。 If you receive the message that you have unreachable code, then there is either no point in it being there, or something you did previously is out of order logically.
如果您收到的消息是您有无法访问的代码,那么它就没有任何意义,或者您之前执行的某些操作在逻辑上是无序的。
The linter rule ie 'no-fallthrough' in eslint, acts as to not allow any accidental fallthrough from case to case. 短绒规则,即在'eslint'中的'no-fallthrough',用于不允许任何意外的摔倒。
Meaning without break code execution will continue from matching case to next cases unless a break, return etc is encountered. 除非遇到中断,返回等,否则没有中断代码执行的含义将从匹配大小写继续到下一个大小写。
Sometimes we do need this but unintentional fallthrough can happen and this rule tries to prevent that. 有时候我们确实需要这个,但是可能会发生无意的崩溃,而这条规则试图阻止这种情况发生。
You can disable the rule or configure it as warning . 您可以禁用该规则或将其配置为警告。 I would recommend to have a variable assigned for return value and at the end of the function and return it without disabling the rule.
我建议为返回值指定一个变量,并在函数结束时返回它而不禁用该规则。
function() {
var returnvalue;
Switch(variableA) {
Case 1:
returnvalue = somevalue;
break;
case 2:
returnvalue = some other value;
break;
default:
returnvalue= default value;
}
return returnvalue;
} }
And for the unreachable part, you are returning from your if else block. 对于无法访问的部分,您将从if else块返回。
So the break will never get any chance to execute. 所以休息永远不会有任何机会执行。
switch (action.type) {
case TYPES.ADD_TO_FAVORITES:
if (state.faves.length <= 10) {
return ...;
} else {
return ...;
}
break; // YOU HAVE ADDED BREAK HERE
default:
return state;
}
Inside case TYPES.ADD_TO_FAVORITES
, either if
or else
will be executed. 在
case TYPES.ADD_TO_FAVORITES
, if
或else
将被执行。 As in both if and else
you have returned some object, the break
you have added just before default
, will never going to be executed! 因为在if和
else
你都返回了一些对象,你在default
之前添加的break
将永远不会被执行!
That's why it says it says unreachable code
. 这就是它说它
unreachable code
。
Return in switch statement make no sense and not allowed . 返回 switch语句 没有意义 , 也不允许 。 You can only return in a function .
您只能在函数中 返回 。 If you want to get value back from switch statement.
如果你想从switch语句中获取值。 Just assign it to a variable like this
只需将它分配给这样的变量即可
var result;
switch (action.type) {
case TYPES.ADD_TO_FAVORITES:
if (state.faves.length <= 10) {
result = assign({}, state, {
faves: state.faves.concat(action.payload),
full: false
});
} else {
result = assign({}, state, {
faves: state.faves,
full: true
});
}
break;
default:
result = state;
break;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.