打破PrototypeJS .each()循环 [英] Breaking out of a PrototypeJS .each() loop

查看:94
本文介绍了打破PrototypeJS .each()循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个非常人为的例子中,我有一个包含3个元素的数组,我正在使用 .each()方法。

In this very contrived example, I have an array with 3 elements that I'm looping over using the .each() method.

 var vals = $w('foo bar baz'); 

 vals.each( function(val) {
    alert(val);

    if( val == 'bar' ) {
        //This exits function(val)
        //but still continues with the .each()
        return;
    }
  });

我可以轻松退回 .each()如果我需要。

I can easily return out of the function being called by .each() if I need to.

我的问题是,如何从.each()函数内部跳出.each()循环打电话?

推荐答案

if( val == 'bar' ) {
    throw $break;
}

它记录在您链接的同一页面上。这是每个功能特别处理的异常。抛出时,它会阻止在其他元素上调用函数。

It's documented at the same page you linked. It's an exception specially handled by the each function. When thrown, it prevents your function from being called on further elements.

这篇关于打破PrototypeJS .each()循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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