码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript Patterns 2.3 - For loops

时间:2014-05-18 19:24:37      阅读:382      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   c   java   

HTMLCollections are objects returned by DOM methods such as:

? document.getElementsByName()

? document.getElementsByClassName()

? document.getElementsByTagName()

   

HTMLCollections, which were introduced before the DOM standard and are still in use today

document.images

All IMG elements on the page

document.links

All A elements

document.forms

All forms

document.forms[0].elements

All fields in the first form on the page

   

// used i+=1 instead of i++ to follow the rule of JSLint

for (var i = 0, max = myarray.length; i < max; i += 1) {

    // do something with myarray[i]

}  

? Use one less variable (no max)

? Count down to 0, which is usually faster because it‘s more efficient to compare to 0 than to the length of the array or to anything other than 0

The first modified pattern is:

bubuko.com,布布扣
var i, myarray = [];

for (i = myarray.length; i--;) {

    // do something with myarray[i]

} 
bubuko.com,布布扣

And the second uses a whileloop:

bubuko.com,布布扣
var myarray = [],
i = myarray.length;

while (i--) {

    // do something with myarray[i]

} 
bubuko.com,布布扣

JavaScript Patterns 2.3 - For loops,布布扣,bubuko.com

JavaScript Patterns 2.3 - For loops

标签:style   blog   class   code   c   java   

原文地址:http://www.cnblogs.com/haokaibo/p/for-loops.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!