JavaScript 非法 break 语句?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22724632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:40:23  来源:igfitidea点击:

JavaScript illegal break statement?

javascript

提问by user3461018

All I have is

我所拥有的是

        if (arr.length == 0)
        {
            document.getElementById("input_table").innerHTML += "<p>Can't sort an empty array, bro!</p>";
            break;
        }

and I'm getting an "Uncaught SyntaxError: Illegal break statement"error from my Chrome console.

而我得到一个"Uncaught SyntaxError: Illegal break statement"从我的浏览器控制台错误。

I don't think I need to post my entire code, since context is irrelevant here.

我认为我不需要发布我的整个代码,因为这里的上下文无关紧要。

回答by lambda2

Break is for loops or switch statements, try a return instead.

Break 是 for 循环或 switch 语句,请尝试返回。

回答by aw04

You need to remove the breakcompletely. Others have stated to use returninstead, but that's a bit misleading. You don't need anything, only your logic to tell when to exit (or the closing bracket).

您需要break完全删除。其他人已经表示要使用return,但这有点误导。您不需要任何东西,只需要您的逻辑来判断何时退出(或结束括号)。