JavaScript forEach方法
最近看了一些html5和js方面的书,受益匪浅,因为看的东西比较多,却都没有怎么静心来做整理,慢慢来吧,可能最近自己有点儿小紧张。今天跟大家分享下JavaScript的forEach方法(其实是从《HTML5程序设计》这本书里看到的一种方法)。
首先说下JavaScript的forEach的标准格式。
为数组中的每个元素执行指定操作。
array1.forEach(callbackfn[, thisArg])
参数
定义
array1
必需。 一个数组对象。
callbackfn
必需。 一个接受最多三个参数的函数。 对于数组中的每个元素,forEach 都会调用callbackfn 函数一次。
thisArg
可选。 可在 callbackfn 函数中为其引用 this 关键字的对象。 如果省略 thisArg,则undefined 将用作 this 值。
如果 callbackfn 参数不是函数对象,则将引发 TypeError 异常。
对于数组中的每个元素,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.prototype
contains the item
method, 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 forEach方法的更多相关文章
- JavaScript forEach() 方法
JavaScript forEach() 方法 JavaScript Array 对象 实例 列出数组的每个元素: <button onclick="numbers.forEach( ...
- javascript forEach方法与jQuery each区别
1.forEach方法 语法: array.forEach(function(currentValue, index, arr), thisValue) 参数: 示例: <!DOCTYPE ht ...
- forEach 方法 (Array) (JavaScript)
为数组中的每个元素执行指定操作. 语法 array1.forEach(callbackfn[, thisArg]) 参数 参数 定义 array1 必选.一个数组对象. callbackfn 必选.最 ...
- JavaScript数组方法--flat、forEach、map
今天到flat了,一个第一次知道该方法还是看到一个面试题,别人给了个答案,用到了flat才知道的方法. 前面也写过关于这道面试题的文章,<一道关于数组的前端面试题>. 这里再来说说吧! f ...
- JavaScript数组方法的兼容性写法 汇总:indexOf()、forEach()、map()、filter()、some()、every()
ECMA Script5中数组方法如indexOf().forEach().map().filter().some()并不支持IE6-8,但是国内依然有一大部分用户使用IE6-8,而以上数组方法又确实 ...
- JavaScript 数组方法总结
最近公司没项目.所以所幸学学JS.毕竟很多人和我一样.属于培训机构出来的.JS基础也很差. 面试的时候面试官问你 .你会JS不.你会毫不犹豫的回答会.因为你确实用过.但是真正会的或许只是以前项目中需要 ...
- 【译】你应该了解的JavaScript数组方法
让我们来做一个大胆的声明:for循环通常是无用的,而且还导致代码难以理解.当涉及迭代数组.查找元素.或对其排序或者你想到的任何东西,都可能有一个你可以使用的数组方法. 然而,尽管这些方法很有用,但是其 ...
- JavaScript数组方法大全(推荐)
原网址:http://www.jb51.net/article/87930.htm 数组在笔试中经常会出现的面试题,javascript中的数组与其他语言中的数组有些不同,为了方便之后数组的方法学习, ...
- javascript遍历方法总结
forEach 循环 JavaScript诞生已经有20多年了,我们一直使用的用来循环一个数组的方法是这样的: for (var index = 0; index < myArray.lengt ...
随机推荐
- ATR的基本结构与意义(无历史字符部分)
Reset 3B FA 13 00 00 81 31 FE 45 4A 43 4F 50 34 31 56 32 32 31 96 复位应答 ATR TS( The Initial character ...
- BZOJ1711: [Usaco2007 Open]Dingin吃饭
1711: [Usaco2007 Open]Dingin吃饭 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 508 Solved: 259[Submit ...
- HBASE API操作问题总结
org.apache.hadoop.hbase.MasterNotRunningException 在centos中查看,发现没有HMaster进程 解决方法: 1.启动hadoop后,需要等一段时间 ...
- Sum Root to Leaf Numbers——LeetCode
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- 关于Cookie和Session【转载】
当你第一次访问一个网站的时候,网站服务器会在响应头内加上Set-Cookie:PHPSESSID=nj1tvkclp3jh83olcn3191sjq3(php服务器),或Set-Cookie JSES ...
- HDU2056(rectangles)
Rectangles Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- css中居中的居中方法
1:给div设置一个宽度,然后添加margin:0 auto属性 div{ width:200px; margin:0 auto; } 2:居中一个浮动元素 确定容器的宽高 宽500 高 300 的层 ...
- Ionic文件目录说明
hooks //google之后这个目录应该是在编译cordova时自定义的脚本命令,方便整合到我们的编译系统和版本控制系统中 plugins //cordova插件的目录,插件的安装下一节详述 ...
- PHP读取文件内容的三种方式
<?php // 第一种读取方式 header("content-type:text/html;charset=utf-8"); // 文件路径 $fileA = " ...
- Django URL传递参数的方法总结
1 无参数情况 配置URL及其视图如下: 1 2 3 4 (r'^hello/$', hello) def hello(request): return HttpResponse("He ...