javascript 在javascript中正确使用continue
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5885911/
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
Correct use of continue in javascript
提问by Dve
I have always believed you use continueas follows:
我一直相信你使用continue如下:
var i;
for (i = 0; i < 10; i++) {
if(i%2===0) {
continue;
}
}
Or
或者
var i, myloop;
myloop: for (i = 0; i < 10; i++) {
if(i%2===0) {
continue myloop;
}
}
But when I run these two snippets of code through JSLint, I get the error:
但是当我通过JSLint运行这两个代码片段时,出现错误:
Problem at line 5 character 5: Unexpected 'continue'. continue;
What am I doing wrong? What is the correct usage?
我究竟做错了什么?正确的用法是什么?
回答by sdleihssirhc
I guess JSLint just hates continue
:
There are almost always better ways of writing statements that more explicitly define what you are attempting to do without resorting to continue. JSLint is all about the good parts, and not about the parts that are acceptable. It forces you to use a higher standard than the one defined.
Douglas says it best in his book:
"The continue statement jumps to the top of the loop. I have never seen a piece of code that was not improved by refactoring it to remove the continue statement."
几乎总是有更好的方法来编写语句,更明确地定义你要做什么,而不用继续。JSLint 是关于好的部分,而不是关于可接受的部分。它迫使您使用比定义的标准更高的标准。
道格拉斯在他的书中说得最好:
“continue 语句跳到循环的顶部。我从来没有见过一段代码没有通过重构它来删除 continue 语句而得到改进。”
回答by Jon
There is an option at the bottom of JSlint that says
JSlint 底部有一个选项说
Tolerate continue
容忍继续
maybe it has issues with it?
也许它有问题?
回答by Andrea
JSLint doesn't like continue
because Douglas doesn't like it.
JSLint 不喜欢,continue
因为 Douglas 不喜欢它。
continue
skips to the next iteration. If you don't insert it, the code will carry on executing. If you want to exit the loop, use break
.
continue
跳到下一次迭代。如果你不插入它,代码将继续执行。如果要退出循环,请使用break
.