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 ...
随机推荐
- IntelliJ IDEA 2017安装和破解方法
一,安装 这里是windows下的安装,另外还有mac和linux下的版本,可自行解决 下载并安装IDEA官网:https://www.jetbrains.com/idea/ 或者百度网盘2017版本 ...
- CAD绘制自定义实体(com接口)
在cad使用过程中,用户可以绘制自定义实体.点击此处下载演示实例. 调用DrawCustomEntity函数,绘制一个自定义实体对象. 下面代码绘制一个自定义实体,C#代码实现如下: private ...
- Android开发使用控件入门--环境搭建
Android开发使用控件入门--环境搭建 软件名称(,梦,,想.CAD ,控件) 1. 环境搭建: 3 1.1. 安装Eclipse 3 1.2. 下载JDK 3 1.3. 下载Android S ...
- 前端axios发送的数据后端接收不到(没有自动依赖注入)可能的原因
前端请求头content-type没有进行正确设置,后端无法解析该类型数据,比如说: 后端若想接收json类型的数据,则需要配置相应的转换器,(spring中可使用@RequestBody注解),若没 ...
- jenkins自动部署测试环境
构建脚本如下: echo "当前目录":$(pwd)echo "当前时间":$(date +%Y-%m-%d_%H:%M)find ./ -type f -na ...
- find命令查找和替换
find命令查找和替换 语法: find -name '要查找的文件名' | xargs perl -pi -e 's|被替换的字符串|替换后的字符串|g' #查找替换当前目录下包含字符串并进行替换 ...
- 【原】Mysql常用语句
1.修改编码方式为UTF-8 ALTER TABLE 表名 CHANGE 列名 新列名 VARCHAR(255) CHARACTER SET utf8 COLLATE ...
- Bullet:ORACLE Using SQL Plan Management(一)
SQL Plan Management如何工作? 当一个SQL硬解析时,基于成本的优化器CBO会生成多个执行计划,并从这些执行计划中选择一个优化器认为最低成本的执行计划. 如果SQL plan bas ...
- 洛谷——P3018 [USACO11MAR]树装饰Tree Decoration
P3018 [USACO11MAR]树装饰Tree Decoration 比较水的一道树上模拟水题,更新每个点的价值为以这个点为根的子树中的价值最小值,同时更新以每个节点为根的$sum$值,即以这个节 ...
- Leetcode 90.子集
子集 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1], [1,2,2], ...