1 array.forEach(callback[, thisArg])
callback在数组每一项上执行的函数,接收三个参数:
thisArg可选参数。用来当作callback 函数内this的值的对象。
forEach 方法按升序为数组中含有效值的每一项执行一次callback 函数,那些已删除(使用delete方法等情况)或者从未赋值的项将被跳过(但不包括那些值为 undefined 的项)。
如果给forEach传递了thisArg 参数,它将作为 callback 函数的执行上下文,类似执行如下函数callback.call(thisArg, element, index, array)。如果 thisArg 值为 undefined 或 null,函数的 this 值取决于当前执行环境是否为严格模式(严格模式下为 undefined,非严格模式下为全局对象)。
1 <script> 2 var arr = [4,3,2,1,2]; 3 arr.forEach(function(correntvalue,index,array){ 4 console.log(correntvalue); //correntvalue是当前项 5 console.log(index); //index是当前项的索引 6 console.log(array); //array是调用forEach的数组 7 }) 8 </script>
原文:http://www.cnblogs.com/mr-yuan/p/6014287.html