這篇文章將為大家詳細講解有關(guān)JavaScript中reduceRight函數(shù)怎么用,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
arr.reduceRight(callback(accumulator, currentValue[, index[, array]])[, initialValue])
Array.prototype.myReduceRight = function(callbackFn, initialValue) { if (typeof callbackFn !== 'function') throw ('callbackFn參數(shù)必須是函數(shù)'); 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; }
關(guān)于“JavaScript中reduceRight函數(shù)怎么用”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。