2010-06-15 135 views
2

我對一些輕早讀拉昇NWmatcher source code,發現這個代碼奇數位我從來沒有在JavaScript見過:這是什麼:main:for(...){...}在做什麼?

main:for(/*irrelevant loop stuff*/){/*...*/} 

這個片段可以在compileGroup方法來發現線441(nwmatcher- 1.1.1)

return new Function('c,s,d,h', 
    'var k,e,r,n,C,N,T,X=0,x=0;main:for(k=0,r=[];e=N=c[k];k++){' + 
     SKIP_COMMENTS + source + 
    '}return r;' 
); 

現在我想出了什麼main:是我自己做的。如果你在一個循環中有一個循環,並且想跳到外循環的下一個迭代(沒有完成內部或外部循環),你可以執行continue main。例如:

// This is obviously not the optimal way to find primes... 
function getPrimes(max) { 
    var primes = [2], //seed 
     sqrt = Math.sqrt, 
     i = 3, j, s; 

    outer: for (; i <= max; s = sqrt(i += 2)) { 
     j = 3; 
     while (j <= s) { 
      if (i % j === 0) { 
       // if we get here j += 2 and primes.push(i) are 
       // not executed for the current iteration of i 
       continue outer; 
      } 
      j += 2; 
     } 
     primes.push(i); 
    } 
    return primes; 
} 

這是什麼叫?
有沒有不支持它的瀏覽器?
continue以外,是否還有其他用途?

回答