由于foreach循环中不像for循环可以直接通过return或break来终止当前循环,不过这里可以借助try...catch。。。来完成
var arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14];
try{
arr.forEach(function(el,index){
if (el==6) {
console.log("try中遇到20,能退出吗?");//
foreach.break=new Error("StopIteration");
}else{
console.log(el);
}
});
}catch(e){
console.log(e.message);
if(e.message==="foreach is not defined") {
console.log("跳出来了?");//
return;
}else throw e;
}