在for循环中我可以使用break或continue.例如:
for(var i=0;i<5;i++){
if(i=3){
continue; //I know this is absurd to use continue here but it's only for example
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我想continue在for循环中的函数内使用该怎么办呢.
例如:
for(var i=0;i<5;i++){
theFunction(i);
}
function theFunction(var x){
if(x==3){
continue;
}
}
Run Code Online (Sandbox Code Playgroud)
我知道这会引发错误.但是,有没有办法让它工作或做类似的事情?
使用该函数的返回值并continue根据它调用:
for (var i = 0; i < 5; i++) {
if (theFunction(i)) {
continue;
}
}
function theFunction(x){
if (x == 3) {
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
顺便提一下你的代码if(i=3){,请注意你需要使用==或者===,单个等号是分配.
| 归档时间: |
|
| 查看次数: |
2644 次 |
| 最近记录: |