2008-09-09 17 views
5

मैं जावास्क्रिप्ट में लूप कैसे बना सकता हूं?मैं जावास्क्रिप्ट में लूप कैसे बना सकता हूं?

+0

कोई भी एसओ के साथ गड़बड़ कर रहा है, बहुत सी चीज़ों को कम करता है, ऐसा लगता है ... –

+0

आह। ठीक है, मुझे लगता है कि इस बारे में हमारे रान ने इस बारे में कुछ बंदूक कूद दी थी। – UnkwnTech

+0

पॉडकास्ट पर इन प्रश्नों को प्रोत्साहित किया गया था, क्योंकि यह साइट को समग्र रूप से मदद करेगा, क्योंकि नेट पर इसे ढूंढने वाले लोग इसे ढूंढेंगे और/या यह Google आंकड़ों के साथ मदद करेगा। – UnkwnTech

उत्तर

24

छोरों में छोरों

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 (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] 
} 

यह बुरा व्यवहार सरणियों से अधिक itterate को for...in छोरों का प्रयोग है। यह ECMA 262 मानक के खिलाफ चला जाता है और जब समस्याएं गैर-मानक विशेषताओं या विधियों को ऐरे ऑब्जेक्ट में जोड़ती हैं, तो समस्याएं पैदा हो सकती हैं, उदा। Prototype द्वारा।

जबकि छोरों

while (myCondition) { 
    // The loop will continue until myCondition is false 
} 
+2

आपको सरणी पर लूप में ... के लिए उपयोग नहीं करना चाहिए। इससे प्रोटोटाइप के साथ समस्याएं पैदा हो जाएंगी। देखें http://www.prototypejs.org/api/array –

+2

यदि आप hasOwnProperty से जांचते हैं तो इन-इन लूप के साथ समस्या से बचा जा सकता है: if (! Things.hasOwnProperty (i)) {जारी रखें; } –

-1

जावास्क्रिप्ट में एक पाश इस तरह दिखता है:

for (var = startvalue; var <= endvalue; var = var + increment) { 
    // code to be executed 
} 
1

यहाँ पाश के लिए एक का एक उदाहरण है:

हम आइटम नोड्स की एक सरणी है।

for(var i = 0; i< nodes.length; i++){ 
    var node = nodes[i]; 
    alert(node); 
} 
0

एक तरफ के फार्म का निर्माण में छोरों (while() ..., do ... while(), 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));

संबंधित मुद्दे