跳转到基于条件的不同开关案例,如goto(Jump to a different switch case based on conditon, like a goto)
我需要根据病情从一种病例转移到另一种病例。 例如,这是我的代码:
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情况。 这个怎么做?
I need to move from one case to another case based on the condition. For example, this is my code:
switch (req.method) { case 'GET': alert('GET METHOD'); break; case 'POST': alert('POST METHOD'); break; case 'PUT': alert('PUT METHOD'); break; default: res.end(); }In the above code, in the POST case I need to check, for example if(A === B), then go to the PUT case like that. How to do this?
最满意答案
进行条件递归
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(); } }Make a conditional recursive
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(); } }更多推荐
发布评论