Breaking out of a PrototypeJS .each() loop

前端 未结 4 1289
难免孤独
难免孤独 2021-02-05 09:52

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( func         


        
4条回答
  •  南方客
    南方客 (楼主)
    2021-02-05 10:37

    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.

提交回复
热议问题