我需要根据情况从一个案例转移到另一个案例。例如,这是我的代码:
switch (req.method) {
case 'GET':
alert('GET METHOD');
break;
case 'POST':
alert('POST METHOD');
break;
case 'PUT':
alert('PUT METHOD');
break;
default:
res.end();
}
在上面的代码中,在POST
的情况下我需要检查,例如if(A === B)
,然后去PUT
这样的情况。如何做到这一点?
最佳答案
条件递归
function checkMethod(method) {
switch (method) {
case 'GET':
alert('GET METHOD');
break;
case 'POST':
alert('POST METHOD');
checkMethod('PUT'); // here stand the pros of a function
break;
case 'PUT':
alert('PUT METHOD');
break;
default:
res.end();
}
}
关于javascript - 根据条件跳转到不同的 switch case,比如 goto,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24904554/