JavaScript でループを作成するにはどうすればよいですか?
			
			15719 次
		
4 に答える
            29        
        
		
ループ用
for (i = startValue; i <= endValue; i++) {
    // Before the loop: i is set to startValue
    // After each iteration of the loop: i++ is executed
    // The loop continues as long as i <= endValue is true
}
for...in ループ
for (i in things) {
    // If things is an array, i will usually contain the array keys *not advised*
    // If things is an object, i will contain the member names
    // Either way, access values using: things[i]
}
for...inループを使用して配列を反復処理するのは悪い習慣です。これはECMA 262標準に反するものであり、 Prototypeなどによって、非標準の属性またはメソッドが Array オブジェクトに追加されると問題が発生する可能性があります。
(コメントでこれを指摘してくれたChase Seibertに感謝します)
while ループ
while (myCondition) {
    // The loop will continue until myCondition is false
}
    于 2008-09-09T15:05:00.387   に答える
    
    
            1        
        
		
for ループの例を次に示します。
itemsノードの配列があります。
for(var i = 0; i< nodes.length; i++){
    var node = nodes[i];
    alert(node);
}
    于 2008-09-09T14:57:59.640   に答える
    
    
            0        
        
		
while() ...組み込みループ ( 、do ... while()、 ) とは別に、3 つの組み込みループ構造なしでループを作成する再帰for() ...とも呼ばれる自己呼び出し関数の構造があります。
次の点を考慮してください。
// set the initial value
var loopCounter = 3;
// the body of the loop
function loop() {
    // this is only to show something, done in the loop
    document.write(loopCounter + '<br>');
    // decrease the loopCounter, to prevent running forever
    loopCounter--;
    // test loopCounter and if truthy call loop() again 
    loopCounter && loop();
}
// invoke the loop
loop();
言うまでもなく、この構造体は戻り値と組み合わせて使用されることが多いため、これは、最初に利用可能ではなく、再帰の最後にある値を処理する方法の小さな例です。
function f(n) {
    // return values for 3 to 1
    //       n   -n  ~-n   !~-n   +!~-n   return
    // conv int neg bitnot  not  number 
    //       3   -3   2    false    0    3 * f(2)
    //       2   -2   1    false    0    2 * f(1)
    //       1   -1   0     true    1        1
    // so it takes a positive integer and do some conversion like changed sign, apply
    // bitwise not, do logical not and cast it to number. if this value is then
    // truthy, then return the value. if not, then return the product of the given
    // value and the return value of the call with the decreased number
    return +!~-n || n * f(n - 1);
}
document.write(f(7));
于 2015-09-02T14:27:40.170   に答える
    
    
            -1        
        
		
JavaScript のループは次のようになります。
for (var = startvalue; var <= endvalue; var = var + increment) {
    // code to be executed
}
    于 2008-09-09T14:55:34.373   に答える