chiyan-lin / code-snippet

the record of something snippety
1 stars 0 forks source link

mock reduce #5

Open chiyan-lin opened 4 years ago

chiyan-lin commented 4 years ago
Array.prototype.new_reduce = function (callback, initData) {
  // 防止数组被修改对先复制一份
  const t = Array.prototype.slice.call(this)
  let start = 1
  let pre = t[0]
  if (initData) {
    pre = initData
    start = 0
  }
  for(let i = start; i < t.length; i++) {
    pre = callback.call(t, pre, t[i], i, t)
  }
  return pre
}

const aa = [1, 2]
console.log('new_reduce', aa.new_reduce(function (a, b) {
  return a + b
}, 5))