我想要的结果是 如果是2 就跳出循环
但是下面的结果是 1 3 4
没有调出循环
请问有办法跳出each这个循环吗??
var a=[{i:1},{i:2},{i:3},{i:4}]
Array.prototype.each= function(fn, bind){
            for (var i = 0, l = this.length; i < l; i++) fn.call(bind, this[i], i, this);
        }
a.each(function(item){
if(item.i==2)return;
console.log(item.i)
})

解决方案 »

  1.   

    这个可以实现 goto  http://www.summerofgoto.com/
    我现在没时间 谁去研究一下?
      

  2.   

            Array.prototype.each2= function(fn, bind){
                for (var i = 0, l = this.length; i < l; i++){
                   if(false===fn.call(bind, this[i], i, this)) return;
                }
            }然后
    if(item.i==2)return false;
    试试
      

  3.   


    var a=[{i:1},{i:2},{i:3},{i:4}]
            Array.prototype.each= function(fn, bind){
                for (var i = 0, l = this.length; i < l; i++) 
                    if(!fn.call(bind, this[i], i, this)){
                        return false;
                    }
            }
            a.each(function(item){
                if(item.i==2)return false;            console.log(item.i)
            })
      

  4.   

    prototype是用抛异常的方法退出来的