javascript 在 jQuery 中使用 break 和 continue
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14251111/
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
Use of break and continue in jQuery
提问by coolguy
I know with core Javascript we can do things like this:
我知道使用核心 Javascript 我们可以做这样的事情:
for(i=1;i<10;i++){
if(i==5){
//break or continue
}
}
But why this is not correct ?
但为什么这是不正确的?
$.each(iteratorarray,function(i){ //iteratorarray - just an array
if(i==5){
//break or continue,will cause error here
}
});
回答by Adil
This behavior is what is implemented by jQuery. This is what jQuery api document say "We can break
the $.each() loop at a particular iteration by making the callback function return false
. Returning non-false
is the same as a continue
statement in a for loop; it will skip immediately to the next iteration", You can read about it here.
这种行为是由 jQuery 实现的。这就是 jQuery api 文档所说的“我们可以break
通过使回调函数 return 来在特定迭代中进行 $.each() 循环false
。返回non-false
与continue
for 循环中的语句相同;它将立即跳到下一次迭代”,你可以在这里阅读它。
回答by Explosion Pills
return
will continue, return false
will break. The documentationtalks about the break
substitute by example, but it still specifies the functionality of return
in the .each
callback.
return
将继续,return false
将打破。该文档break
通过示例讨论了替代,但它仍然指定return
了.each
回调中的功能。
回答by Bergi
why this is not correct?
为什么这是不正确的?
break
or continue
statements are only valid as a part of a (for-, for-in-, while-, …) loop statement. You cannot do that in a plain function. From the spec:
break
orcontinue
语句仅作为 (for-, for-in-, while-, ...)循环语句的一部分有效。你不能在一个普通的函数中做到这一点。从规范:
A program is considered syntactically incorrect if […] the program contains a continue/break statement […] directly or indirectly (but not crossing function boundaries), within an
IterationStatement
.
如果[...]程序包含继续/ break语句[...]直接或间接(A程序被认为是语法不正确,但不跨越函数边界),之内的
IterationStatement
。
Instead, you can return
from a function (which breaks further execution of the function body). As you can read in the documentation, returning false
stops the iteration in $.each
.
相反,您可以return
从一个函数(这会中断函数体的进一步执行)。正如您在文档中所读到的,返回会false
停止$.each
.
回答by Arun P Johny
$.each
uses a callback to execute the custom code, break
is a control statement which can disrupt the wrapping loop. The break
or continue
has to be call directly from the loop
body not from a delegated function.
$.each
使用回调来执行自定义代码,break
是一个可以破坏包装循环的控制语句。该break
或continue
有直接从电话是loop
身体不委派功能。
$.each(iteratorarray,function(i){ //iteratorarray - just an array
if(i==5){
//break or continue,will cause error here
}
});
Can be considered as equals to
可以认为等于
for(i=0;i<iteratorarray.length;i++){
if(callback(i, iteratorarray[i]) === false){
break;
}
}
Here your code is residing inside the callback()
function therefore you cannot use loop control
statements inside the callback.
这里您的代码驻留在callback()
函数内,因此您不能loop control
在回调内使用语句。
You can read more in the documentation.
回答by Snakes and Coffee
In the $.each
case, there is no loop you can break out of, and that's why you get the error. The way jQuery does it, however, is that the loop breaks
if your function returns false
.
在这种$.each
情况下,您无法跳出循环,这就是您收到错误的原因。然而,jQuery 的做法是,breaks
如果您的函数返回false
.
回答by Arvind Bhardwaj
You can also use JavaScript's everymethod for iterating array elements.
您还可以使用 JavaScript 的every方法来迭代数组元素。
The every method calls the callbackfn function one time for each array element, in ascending index order, until the callbackfn function returns false. If an element that causes callbackfn to return false is found, the every method immediately returns false. Otherwise, the every method returns true.
every 方法为每个数组元素调用一次 callbackfn 函数,按索引升序,直到 callbackfn 函数返回 false。如果找到导致 callbackfn 返回 false 的元素,则 every 方法立即返回 false。否则,every 方法返回true。