gpt4 book ai didi

javascript - 优化嵌套 switch case

转载 作者:行者123 更新时间:2023-12-02 14:45:46 24 4
gpt4 key购买 nike

由于嵌套 switch case 的可读性不太好,我如何优化我的嵌套 switch case?

switch (code) {
case 200:
switch (status) {
case 'EXISTS':
$alreadyExistsMessage.show();
break;
case 'UPDATED':
$alreadyExistsMessage.show();
break;
case 'ERROR':
$wrongFormatMessage.show();
break;
}
break;
case 201:
redirectOnSuccess(data);
break;
default:
$wrongFormatMessage.show();
break;
}

最佳答案

我唯一要做的就是将 'UPDATED''EXISTS' 由于同一个 block 而合并:

switch (code) {
case 200:
switch (status) {
case 'EXISTS':
case 'UPDATED':
$alreadyExistsMessage.show();
break;
case 'ERROR':
$wrongFormatMessage.show();
break;
}
break;
case 201:
redirectOnSuccess(data);
break;
default:
$wrongFormatMessage.show();
break;
}

关于javascript - 优化嵌套 switch case,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36624021/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com