这篇文章将为大家详细讲解有关JavaScript中reduceRight函数怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

arr.reduceRight(callback(accumulator, currentValue[, index[, array]])[, initialValue])
Array.prototype.myReduceRight = function(callbackFn, initialValue) {
if (typeof callbackFn !== 'function') throw ('callbackFn参数必须是函数');
let element = this,
len = element.length || 0,
index = len - 1,
result;
if (arguments.length >= 2) {
result = arguments[1];
} else {
while (index >= 0 && !(index in element)) {
index--;
}
if (index < 0) {
throw new TypeError('reduceRight of empty array with no initial value');
}
result = element[index--];
}
for (; index >= 0; index--) {
if (index in element) {
result = callbackFn(result, element[index], index, element);
}
}
return result;
}关于“JavaScript中reduceRight函数怎么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。