Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

javascript - 在JavaScript中切换语句多个案例(Switch statement multiple cases in JavaScript)

I need multiple cases in switch statement in JavaScript, Something like:(我需要在JavaScript的switch语句中使用多种情况,例如:)

switch (varName) { case "afshin", "saeed", "larry": alert('Hey'); break; default: alert('Default case'); break; } How can I do that?(我怎样才能做到这一点?) If there's no way to do something like that in JavaScript, I want to know an alternative solution that also follows DRY concept .(如果没有办法在JavaScript中执行类似的操作,我想知道一个也遵循DRY概念的替代解决方案。)   ask by Afshin Mehrabani translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use the fall-through feature of the switch statement.(使用switch语句的直通功能。)

A matched case will run until a break (or the end of the switch statement) is found, so you could write it like:(匹配的大小写将一直运行到找到break (或switch语句的末尾)为止,因此您可以这样写:) switch (varName) { case "afshin": case "saeed": case "larry": alert('Hey'); break; default: alert('Default case'); }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...