JavaScript中的forEach、$.each、map方法

来源:互联网 发布:捕鱼刷分软件 编辑:程序博客网 时间:2024/06/08 07:59

forEach()

Array在ES5新增的方法中,参数都是function类型,默认有传参,forEach方法中的function回调支持3个参数,第1个是遍历的数组内容;第2个是对应的数组索引,第3个是数组本身。

因此:

[].forEach(function(value,index,array){});


jQuery中的$.each()

$.each(function(value,index,array){});


map()

[].map(function(value,index,array){});


注意:由于forEach、map都是ECMA5新增数组的方法,所以ie9以下的浏览器还不支持,不过,可以从Array原型扩展可以实现以上全部功能,例如forEach方法:

if(typeof Array.prototype.forEach != "function"){

     Array.prototype.forEach = function(){

         //实现 

     }

}


0 0