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. asp.net mvc,基于aop实现的接口访问统计、接口缓存等

    其实asp.net 上aop现有的框架应该蛮多的,比如静态注入式的PostSharp(新版本好像已经商业化了,旧版本又不支持.net4.0+),或者通过反射的(性能会降低). 本文则是通过mvc其中一 ...

  2. 记VS2008安装及使用及卸载的艰辛历程!!!(2018/11/6-2018/11/14)

    此文为了纪念我对VS2008“孜孜不倦的”无数次的安装及卸载,以及解决使用过程中出现的问题所花费的人力物力和财力!成功之后再作补充.

  3. 日常操作之如何打开windows注册表

    1.打开注册表:第一步按“win+R”或者点击开始菜单,找到运行,在运行输入框里面输入“regedit”.

  4. 访问请求参数request.getParameter()

    访问请求参数request.getParameter() 制作人:全心全意 getParameter() 例: 传递参数页: <%@ page language="java" ...

  5. C. Day at the Beach

    codeforces 599c C. Day at the Beach One day Squidward, Spongebob and Patrick decided to go to the be ...

  6. Maven_自动化构建和构建环节

    [构建过程的几个主要环节] ①清理:删除以前的编译结果,为重新编译做好准备. ②编译:将 Java 源程序编译为字节码文件. ③测试:针对项目中的关键点进行测试,确保项目在迭代开发过程中关键点的正确性 ...

  7. [luoguP1433] 吃奶酪(DP || Dfs)

    传送门 深搜加剪纸可A(O(玄学) 1274ms) ——代码 #include <cmath> #include <cstdio> #include <iostream& ...

  8. [luoguP2904] [USACO08MAR]跨河River Crossing(DP)

    传送门 f[i] 表示送前 i 头牛过去再回来的最短时间 f[i] = min(f[i], f[j] + sum[i - j] + m) (0 <= j < i) ——代码 #includ ...

  9. Codeforces Round #239(Div. 2) 做后扯淡玩

    今天补了下 cf 239div2 顿时信心再度受挫 老子几乎已经木有时间了啊 坐着等死的命.哎!!! 到现在还只能做大众题,打铁都不行. 每次D题都是有思路敲错,尼玛不带这么坑爹的. 哎!不写了,写这 ...

  10. 使用ajax,后台传回的数据处理

    使用ajax,后台传到前台的是json数据,但这只是一个字符串的形式,需要进行转换为对象 转换后再使用$.each()方法进行遍历 使用alert(typeof  msg) ;  可以查看msg的类型 ...