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 ...
随机推荐
- VMware 12虚拟机下Ubuntu 16连不上网解决方法
打开自带Firefox浏览器,显示连接不上网,终端下 ping 也显示 unkown 解决方法: 1.打开虚拟机的“编辑”选项,选择“虚拟网络编辑器” 2.选择VMnet8(我不知道为啥VMnet ...
- radiobutton group
1. 环境:VS2010 2. 分组 将radio1.radio2.radio3分为1组,radio4.radio5分为另一组: 方法:设置 radio1 的 属性: group.tabstop ...
- 梦想CAD控件COM接口标注样式
增加标注样式 用户可以增加标注样式到数据库,具体实现c#代码如下: private void CreateDim() { //返回控件的数据库对象 MxDrawDatabase database = ...
- java虚拟机(五)--垃圾回收机制GC5
什么样的对象需要回收 如果对象已经死亡了,就可以进行回收,判断方式如下 1).引用计数器:给对象添加一个计数器,有地方引用,就+1,当引用失效,就-1.当计数器为0时,判断对象不能再使用,但是当对象相 ...
- BeanFactory的生命周期
Bean自身的方法:调用Bean构造函数实例化Bean.调用setter设置Bean的属性值及通过<beam=n>的init-method和destory-method所制定的方法. Be ...
- 经典书籍---MySQL经典书籍下载
以下是一些经典的MySQL书籍电子版,括号内为提取码,若需自取. 欢迎阅读纸质版,尊重作者版权 高性能MySQL_中文版 [ hre3 ] 高性能MySQL_英文版[ m2xj ] MySQL技术内幕 ...
- gulp-file-include 合并 html 文件
gulp-file-include 是 gulp 插件,它提供了一个 include 方法让我们可以像后端模板那样把公共部分的页面导入进来. 安装依赖包(包括了 gulp-file-include 和 ...
- php正则表达式匹配html标签
用php正则表达式找出div标签,div允许多层嵌套,比如在以下文本中找出class为quizPutTag的div? <html> <head></head> &l ...
- Docker学习总结(17)——学会使用Dockerfile
Docker.Dockerfile.Docker镜像.容器这些都是什么鸟? 老生常谈,再再再--普及一下: Docker: 最早是dotCloud公司出品的一套容器管理工具,但后来Docker慢慢火起 ...
- mySQL and sqoop for ubuntu
数据的导入导出 ——MySQL & sqoop in Ubuntu 1.完成搭建hadoop集群 2.安装MySQL sudo apt-get install mysql-server mys ...