four tabs indent FTW


SUBMITTED BY: inTHEpast

DATE: April 15, 2017, 11:14 a.m.

FORMAT: JavaScript

SIZE: 2.3 kB

HITS: 671

  1. [log]
  2. // Production steps of ECMA-262, Edition 5, 15.4.4.21
  3. // Reference: http://es5.github.io/#x15.4.4.21
  4. // https://tc39.github.io/ecma262/#sec-array.prototype.reduce
  5. if (!Array.prototype.reduce) {
  6. Object.defineProperty(Array.prototype, 'reduce', {
  7. value: function(callback /*, initialValue*/) {
  8. if (this === null) {
  9. throw new TypeError( 'Array.prototype.reduce ' +
  10. 'called on null or undefined' );
  11. }
  12. if (typeof callback !== 'function') {
  13. throw new TypeError( callback +
  14. ' is not a function');
  15. }
  16. // 1. Let O be ? ToObject(this value).
  17. var o = Object(this);
  18. // 2. Let len be ? ToLength(? Get(O, "length")).
  19. var len = o.length >>> 0;
  20. // Steps 3, 4, 5, 6, 7
  21. var k = 0;
  22. var value;
  23. if (arguments.length >= 2) {
  24. value = arguments[1];
  25. } else {
  26. while (k < len && !(k in o)) {
  27. k++;
  28. }
  29. // 3. If len is 0 and initialValue is not present,
  30. // throw a TypeError exception.
  31. if (k >= len) {
  32. throw new TypeError( 'Reduce of empty array ' +
  33. 'with no initial value' );
  34. }
  35. value = o[k++];
  36. }
  37. // 8. Repeat, while k < len
  38. while (k < len) {
  39. // a. Let Pk be ! ToString(k).
  40. // b. Let kPresent be ? HasProperty(O, Pk).
  41. // c. If kPresent is true, then
  42. // i. Let kValue be ? Get(O, Pk).
  43. // ii. Let accumulator be ? Call(
  44. // callbackfn, undefined,
  45. // « accumulator, kValue, k, O »).
  46. if (k in o) {
  47. value = callback(value, o[k], k, o);
  48. }
  49. // d. Increase k by 1.
  50. k++;
  51. }
  52. // 9. Return accumulator.
  53. return value;
  54. }
  55. });
  56. }

comments powered by Disqus