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

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

Use of break and continue in jQuery

javascriptjquery

提问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 breakthe $.each() loop at a particular iteration by making the callback function return false. Returning non-falseis the same as a continuestatement 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-falsecontinuefor 循环中的语句相同;它将立即跳到下一次迭代”,你可以在这里阅读它。

回答by Explosion Pills

returnwill continue, return falsewill break. The documentationtalks about the breaksubstitute by example, but it still specifies the functionality of returnin the .eachcallback.

return将继续,return false将打破。该文档break通过示例讨论了替代,但它仍然指定return.each回调中的功能。

回答by Bergi

why this is not correct?

为什么这是不正确的?

breakor continuestatements 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:

breakorcontinue语句仅作为 (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 returnfrom a function (which breaks further execution of the function body). As you can read in the documentation, returning falsestops the iteration in $.each.

相反,您可以return从一个函数(这会中断函数体的进一步执行)。正如您在文档中所读到,返回会false停止$.each.

回答by Arun P Johny

$.eachuses a callback to execute the custom code, breakis a control statement which can disrupt the wrapping loop. The breakor continuehas to be call directly from the loopbody not from a delegated function.

$.each使用回调来执行自定义代码,break是一个可以破坏包装循环的控制语句。该breakcontinue有直接从电话是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 controlstatements inside the callback.

这里您的代码驻留在callback()函数内,因此您不能loop control在回调内使用语句。

You can read more in the documentation.

您可以在文档中阅读更多内容

回答by Snakes and Coffee

In the $.eachcase, 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 breaksif 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。