JavaScript forEach方法
2015-07-17 10:19
597 查看
最近看了一些html5和js方面的书,受益匪浅,因为看的东西比较多,却都没有怎么静心来做整理,慢慢来吧,可能最近自己有点儿小紧张。今天跟大家分享下JavaScript的forEach方法(其实是从《HTML5程序设计》这本书里看到的一种方法)。
首先说下JavaScript的forEach的标准格式。
为数组中的每个元素执行指定操作。
[/code]
如果 callbackfn 参数不是函数对象,则将引发 TypeError 异常。
对于数组中的每个元素,forEach 方法都会调用 callbackfn 函数一次(采用升序索引顺序)。 不为数组中缺少的元素调用该回调函数。
除了数组对象之外,forEach 方法可由具有 length 属性且具有已按数字编制索引的属性名的任何对象使用。
function callbackfn(value, index, array1)
可使用最多三个参数来声明回调函数。
回调函数的参数如下所示。
好吧,上面是从微软的http://technet.microsoft.com/zh-cn/ff679980%28v=vs.85%29页面copy过来的,有兴趣的直接去那里看就好了。也就是说一般方法的格式是:
arrayx.forEach(function(value,index,arrayy){…})
但对于NodeList要用下面的写法。
[].forEach.call(lists,function(valule.index.arrayy){…})
JavaScript has an inheritance mechanism based on prototypes. Array instances inherit array methods (such as forEach or map) because their prototype chain looks like the following:
forEach, map and the likes are own properties of the Array.prototype object.
Unlike arrays, NodeList prototype chain looks like the following:
localStorage['bucket' + i] = elem.getAttribute('data-bucket');
});
首先说下JavaScript的forEach的标准格式。
为数组中的每个元素执行指定操作。
array1.forEach(callbackfn[, thisArg])
[/code]
参数 | 定义 |
---|---|
array1 | 必需。 一个数组对象。 |
callbackfn | 必需。 一个接受最多三个参数的函数。 对于数组中的每个元素,forEach 都会调用 callbackfn 函数一次。 |
thisArg | 可选。 可在 callbackfn 函数中为其引用 this 关键字的对象。 如果省略 thisArg,则 undefined 将用作 this 值。 |
对于数组中的每个元素,forEach 方法都会调用 callbackfn 函数一次(采用升序索引顺序)。 不为数组中缺少的元素调用该回调函数。
除了数组对象之外,forEach 方法可由具有 length 属性且具有已按数字编制索引的属性名的任何对象使用。
回调函数语法
回调函数的语法如下所示:function callbackfn(value, index, array1)
可使用最多三个参数来声明回调函数。
回调函数的参数如下所示。
回调参数 | 定义 |
---|---|
value | 数组元素的值。 |
index | 数组元素的数字索引。 |
array1 | 包含该元素的数组对象。 |
修改数组对象
forEach 方法不直接修改原始数组,但回调函数可能会修改它。好吧,上面是从微软的http://technet.microsoft.com/zh-cn/ff679980%28v=vs.85%29页面copy过来的,有兴趣的直接去那里看就好了。也就是说一般方法的格式是:
arrayx.forEach(function(value,index,arrayy){…})
但对于NodeList要用下面的写法。
[].forEach.call(lists,function(valule.index.arrayy){…})
Why can’t I use forEach or map on a NodeList?
NodeList are used very much like arrays and it would be tempting to use Array.prototype methods on them. This is, however, impossible.JavaScript has an inheritance mechanism based on prototypes. Array instances inherit array methods (such as forEach or map) because their prototype chain looks like the following:
myArray --> Array.prototype --> Object.prototype --> null(the prototype chain of an object can be obtained by calling several times Object.getPrototypeOf)
forEach, map and the likes are own properties of the Array.prototype object.
Unlike arrays, NodeList prototype chain looks like the following:
myNodeList --> NodeList.prototype --> Object.prototype --> null
NodeList.prototypecontains the
itemmethod, but none of the Array.prototype methods, so they cannot be used on NodeLists.
实例
[].forEach.call(document.querySelectorAll('section[data-bucket]'), function(elem, i) {localStorage['bucket' + i] = elem.getAttribute('data-bucket');
});
相关文章推荐
- JavaScript内置对象
- 获取元素长度、宽度并改变其值的js
- js 数字前面自动补零
- js 关于table的操作
- boost库解析json的例子
- javascript : json对象的排序
- javascript性能优化
- js的2种继承方式详解
- js调用打印机打印指定内容
- jsonp跨域原理解析
- a标签 中调用js的几种方法
- js常用插件
- 使用java和javascript判断当前浏览者的操作系统
- JavaScript调用浏览器打印功能实例分析
- 用javascript判断一个html元素是否存在的五种方法:
- 使用 jsoup 对 HTML 文档进行解析和操作
- json 数据类型
- [JSOI2010][BZOJ1822] Frozen Nova 冷冻波
- js常用插件
- JS判断页面是否出现滚动条的方法