map() 方法
map() 方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值。
语法:
array.map(function(currentValue,index,arr), thisValue)
callback
为数组中每个元素执行的函数,该函数可接受1-3个参数currentvalue
参数表示数组的当前元素项,必须的参数index
参数表示的当前元素下标,可选参数arr
参数表示当前元素所属的数组,可选参数
thisValue
表示执行回调函数callback()
时的this
指向。可选参数。当不写时,则默认是指向window
全局
手写 map
简单版
1 | Array.prototype._map = function (fn) { |
完全版
1 | Array.prototype.__map = function (fn, ctx) { |
reduce 版
1 | Array.prototype.___map = function (fn, ctx) { |