We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
reduce函数的第一个参数是一个回调函数,第二个参数为可选的初始值。
reduce
如果有初始值的话,回调函数就会从数组的第0项开始执行,也就是会执行arr.length次;
arr.length
但是如果没有初始值的话,会默认取数组的第0项为初始值,回调函数会从数组的第1项开始执行,也就是会执行arr.length - 1次。
arr.length - 1
这点从我们手写一个reduce的实现就可以看出来,代码如下:
Array.prototype.MyReduce = function (fn, initialValue) { var arr = Array.prototype.slice.call(this); var pre, startIndex; pre = initialValue ? initialValue : arr[0]; startIndex = initialValue ? 0 : 1; for (var i = startIndex; i < arr.length; i++) { pre = fn.call(null, pre, arr[i], i, this) } return pre }
过程分析:
map、reduce
MyReduce
Array.prototype
ary.MyReduce()
ary是一个类似于这样的数组[1, 2, 3]
var arr = ...
this
ary
var arr = this;
Array.prototype.slice.call(this)
pre
initialValue
0
initialValue ?
false
startIndex
1
fn
for
fn.call()
The text was updated successfully, but these errors were encountered:
"至于为什么不使用var arr = this;的方式而是使用Array.prototype.slice.call(this),算是实现一个浅拷贝吧,因为reduce是不会改变原数组的。"不想影响原数组的话, 那岂不是应该用深拷贝来拷贝数组吗
Sorry, something went wrong.
No branches or pull requests
reduce
函数的第一个参数是一个回调函数,第二个参数为可选的初始值。如果有初始值的话,回调函数就会从数组的第0项开始执行,也就是会执行
arr.length
次;但是如果没有初始值的话,会默认取数组的第0项为初始值,回调函数会从数组的第1项开始执行,也就是会执行
arr.length - 1
次。这点从我们手写一个
reduce
的实现就可以看出来,代码如下:过程分析:
map、reduce
这种方法都是数组原型对象上的方法,所以我将MyReduce
定义在Array.prototype
上,这样你就可以直接使用ary.MyReduce()
这样的方式调用它了(ary是一个类似于这样的数组[1, 2, 3]
)。reduce
,它接收的第一个参数是一个回调函数,第二个是初始值var arr = ...
的作用是获取调用MyReduce
函数的那个变量,也就是说this
会指向那个变量,例如ary.MyReduce()
,那么此时this
就为ary
。var arr = this;
的方式而是使用Array.prototype.slice.call(this)
,算是实现一个浅拷贝吧,因为reduce
是不会改变原数组的。reduce
中的回调函数的第一个参数pre
,也就是上一次运行结果的返回值,可以看到这里就用到了初始值initialValue
,如果存在初始值就取初始值,不存在则默认取数组第0
项。(当然这里直接用initialValue ?
来判断存不存在并不准确,因为我们知道0
也会被判断为false
)startIndex
,若是不存在初始值,则初始值是会取数组中的第0
项的,相当于第0
项并不需要运行,所以startIndex
会是1
,而如果有初始值的话则需要将数组的每一项都经过fn
运行一下。for
循环中使用fn.call()
来调用fn
函数,并且最后一个参数是要把原来的数组传递到回调函数中,也就是这里的this
。The text was updated successfully, but these errors were encountered: