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:

var i, myarray = [];

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

    // do something with myarray[i]

} 

And the second uses a whileloop:

var myarray = [],
i = myarray.length; while (i--) { // do something with myarray[i] }

JavaScript Patterns 2.3 For loops的更多相关文章

  1. JavaScript Patterns 7.1 Singleton

    7.1 Singleton The idea of the singleton pattern is to have only one instance of a specific class. Th ...

  2. JavaScript Patterns 6.7 Borrowing Methods

    Scenario You want to use just the methods you like, without inheriting all the other methods that yo ...

  3. JavaScript Patterns 6.6 Mix-ins

    Loop through arguments and copy every property of every object passed to the function. And the resul ...

  4. JavaScript Patterns 6.5 Inheritance by Copying Properties

    Shallow copy pattern function extend(parent, child) { var i; child = child || {}; for (i in parent) ...

  5. JavaScript Patterns 6.4 Prototypal Inheritance

    No classes involved; Objects inherit from other objects. Use an empty temporary constructor function ...

  6. JavaScript Patterns 6.3 Klass

    Commonalities • There’s a convention on how to name a method, which is to be considered the construc ...

  7. JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance

    // the parent constructor function Parent(name) { this.name = name || 'Adam'; } // adding functional ...

  8. JavaScript Patterns 6.1 Classical Versus Modern Inheritance Patterns

    In Java you could do something like: Person adam = new Person(); In JavaScript you would do: var ada ...

  9. JavaScript Patterns 5.9 method() Method

    Advantage Avoid re-created instance method to this inside of the constructor. method() implementatio ...

随机推荐

  1. iOS,Core Animation--负责视图的复合功能

    简介 UIKit API UIKit是一组Objective-C API,为线条图形.Quartz图像和颜色操作提供Objective-C 封装,并提供2D绘制.图像处理及用户接口级别的动画.    ...

  2. JavaScipt30(第十个案例)(主要知识点:选中一个数组中间相连部分进行操作的一种思路)

    承接上文,第九个案例就不说了,是控制台的一些东西,一般用的很少,了解下就行了,想用的时候再翻api.这是第10个案例: 需要实现的效果是:点击一个checkbox,然后按下shift点击另一个chec ...

  3. 若要允许 GET 请求,请将 JsonRequestBehavior 设置为 AllowGet(转载)

    若要允许 GET 请求,请将 JsonRequestBehavior 设置为 AllowGet 若要允许 GET 请求,请将 JsonRequestBehavior 设置为 AllowGet 请将 J ...

  4. 搜索--P1219 N皇后

    题目描述 检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行.每列有且只有一个,每条对角线(包括两条主对角线的所有平行线)上至多有一个棋子. 上面的布局可以用序列2 4 6 1 3 ...

  5. [USACO06JAN] 冗余路径 Redundant Paths

    题目描述 In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1. ...

  6. [USACO15JAN]Grass Cownoisseur

    \(tarjan\)缩点+\(DAG\)上最长路. 求一个以\(1\)为起点的最长路和一个以\(1\)为终点的最长路,然后找那个逆行边就行了. 然后这个我\(RE\)了好久,原因是\(vector\) ...

  7. chrome://plugins 无法打开的解决方法,同时解决“该网页已屏蔽插件-adobe flash player”

    chrome打开想要看视频时提示该网页已屏蔽插件-adobe flash player,在网上查了半天说在chrome plugins里面打开就可以了.可是chrome://plugins 无法打开, ...

  8. Python 实现批量查询IP并解析为归属地

    一.背景: 最近工作中做了一个小功能,目的是为了分析注册用户区域分布和订单的区域分布情况.所以需要将其对应的IP信息解析为归属地,并同步每天同步更新.线上跑起来效率还是有优化的空间,优化的方向:在调用 ...

  9. Java Arrays.sort相关用法与重载

    Java Arrays.sort() Java中的数组排序函数, 头文件 import java.util.Arrays; 相关API Arrays.sort(arys[]) Arrays.sort( ...

  10. PAT 1130 Infix Expression

    Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...