JavaScript Patterns 2.3 For loops
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的更多相关文章
- 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 ...
- JavaScript Patterns 6.7 Borrowing Methods
Scenario You want to use just the methods you like, without inheriting all the other methods that yo ...
- JavaScript Patterns 6.6 Mix-ins
Loop through arguments and copy every property of every object passed to the function. And the resul ...
- JavaScript Patterns 6.5 Inheritance by Copying Properties
Shallow copy pattern function extend(parent, child) { var i; child = child || {}; for (i in parent) ...
- JavaScript Patterns 6.4 Prototypal Inheritance
No classes involved; Objects inherit from other objects. Use an empty temporary constructor function ...
- JavaScript Patterns 6.3 Klass
Commonalities • There’s a convention on how to name a method, which is to be considered the construc ...
- JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance
// the parent constructor function Parent(name) { this.name = name || 'Adam'; } // adding functional ...
- 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 ...
- JavaScript Patterns 5.9 method() Method
Advantage Avoid re-created instance method to this inside of the constructor. method() implementatio ...
随机推荐
- vue城市三级联动组件 vue-area-linkage
Install the pkg with npm: // v5之前的版本 npm i --save vue-area-linkage // v5及之后的版本 npm i --save vue-area ...
- AdMob设计工具google web designer
一.google web designer工具中文文档: https://support.google.com/webdesigner?hl=zh-Hans#topic=3227692 我用的版本:应 ...
- arx刷新图形界面
actrTransactionManager->flushGraphics(); acedUpdateDisplay();
- docker常用命令理解
docker help Commands: attach Attach local standard input, output, and error streams to a running con ...
- java Object类中方法介绍
- JS函数assign
Object函数提供了一个叫做assign的函数,用来合并多个对象. Object.assign(...): 你可以传递多个对象给该函数,这些对象中的自有且可枚举的属性,会被拷贝给第一个对象. var ...
- Description Resource Path Location Type Missing artifact com.********:framework:jar:1.0.2 pom.xml /项目名 line **** Maven Dependency Problem
问题具体描述如下图所示: 对于该问题本人是这么解决的. 在window下[Preferences]目录找到[Maven]下的[usersetting] 查看local repository 里面的路径 ...
- 09js、MySQL相关
09js.MySQL相关-2018/07/19 1.js的dom 理解一下文档对象模型:html文件加载到内存之后会形成一颗dom树,根据这些节点对象可以进行脚本代码的动态修改;在dom树当中 一切皆 ...
- Pycharm Anaconda 安装dlib
由于采用python3.7安装会出现各种问题,两种解决方法. 1)安装Cmake boost等(不推荐,麻烦且不容易成功). 2)安装Anaconda,创建一个python3.6的环境. 这里使用第二 ...
- buf.readInt8()
buf.readInt8(offset[, noAssert]) offset {Number} 0 noAssert {Boolean} 默认:false 返回:{Number} 从该 Buffer ...