Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mock reduce #5

Open
chiyan-lin opened this issue May 17, 2020 · 0 comments
Open

mock reduce #5

chiyan-lin opened this issue May 17, 2020 · 0 comments

Comments

@chiyan-lin
Copy link
Owner

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))


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant