在Javascript中从开关盒内部打破循环 [英] Break for loop from inside of switch case in Javascript

查看:138
本文介绍了在Javascript中从开关盒内部打破循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用什么命令才能退出for循环,也可以从 //里面的代码直接跳转到 //

  //在
之前的代码(b中的$ a $)
{
switch(something)
{
case something:
{
//代码
break;





$ b $ div $ =不幸的是,Javascript没有允许 break ing通过多个级别。最简单的方法是通过创建一个匿名函数来利用 return 语句的功能:



<$ p $ (
$($ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
{
//
return;
}
}
}
}());
//
之后的代码

这是因为 return 离开函数,因此隐式离开循环,直接移动到代码之后


What command I must use, to get out of the for loop, also from //code inside jump direct to //code after

//code before
for(var a in b)
    {
    switch(something)
        {
        case something:
            {
            //code inside
            break;
            }
        }
    }
//code after

解决方案

Unfortunately, Javascript doesn't have allow breaking through multiple levels. The easiest way to do this is to leverage the power of the return statement by creating an anonymous function:

//code before
(function () {
    for (var a in b) {
        switch (something) {
        case something:
            {
                //code inside
                return;
            }
        }
    }
}());
//code after

This works because return leaves the function and therefore implicitly leaves the loop, moving you straight to code after

这篇关于在Javascript中从开关盒内部打破循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆