You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Production steps of ECMA-262, Edition 5, 15.4.4.21// Reference: http://es5.github.io/#x15.4.4.21// https://tc39.github.io/ecma262/#sec-array.prototype.reduceif(!Array.prototype.reduce){Object.defineProperty(Array.prototype,'reduce',{value: function(callback/*, initialValue*/){if(this===null){thrownewTypeError('Array.prototype.reduce '+'called on null or undefined');}if(typeofcallback!=='function'){thrownewTypeError(callback+' is not a function');}// 1. Let O be ? ToObject(this value).varo=Object(this);// 2. Let len be ? ToLength(? Get(O, "length")).varlen=o.length>>>0;// Steps 3, 4, 5, 6, 7 vark=0;varvalue;if(arguments.length>=2){value=arguments[1];}else{while(k<len&&!(kino)){k++;}// 3. If len is 0 and initialValue is not present,// throw a TypeError exception.if(k>=len){thrownewTypeError('Reduce of empty array '+'with no initial value');}value=o[k++];}// 8. Repeat, while k < lenwhile(k<len){// a. Let Pk be ! ToString(k).// b. Let kPresent be ? HasProperty(O, Pk).// c. If kPresent is true, then// i. Let kValue be ? Get(O, Pk).// ii. Let accumulator be ? Call(// callbackfn, undefined,// « accumulator, kValue, k, O »).if(kino){value=callback(value,o[k],k,o);}// d. Increase k by 1. k++;}// 9. Return accumulator.returnvalue;}});}
Array.prototype.reduce
语法:
参数
1)callback 中的参数
(可选)
(可选)
2)initialValue:用作第一个调用 callback的第一个参数的值。 如果没有提供初始值,则将使用数组中的第一个元素。 在没有初始值的空数组上调用 reduce 将报错
返回值
函数累计处理的结果
Polyfill
Reference
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
The text was updated successfully, but these errors were encountered: