Skip to content
New issue

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

Polyfill of「reduce」 #58

Closed
PolluxLee opened this issue Jun 18, 2018 · 0 comments
Closed

Polyfill of「reduce」 #58

PolluxLee opened this issue Jun 18, 2018 · 0 comments
Labels

Comments

@PolluxLee
Copy link
Owner

PolluxLee commented Jun 18, 2018

Array.prototype.reduce

reduce() 方法对累加器和数组中的每个元素(从左到右)应用一个函数,将其减少为单个值

语法:

arr.reduce(callback[, initialValue]);

参数

1)callback 中的参数

参数 描述
accumulator 累加器累加回调的返回值; 它是上一次调用回调时返回的累积值,或 initialValue
currentValue 数组中正在处理的元素
currentIndex
(可选)
数组中正在处理的当前元素的索引。 如果提供了 initialValue,则索引号为0,否则为索引为1
array
(可选)
调用reduce的数组

2)initialValue:用作第一个调用 callback的第一个参数的值。 如果没有提供初始值,则将使用数组中的第一个元素。 在没有初始值的空数组上调用 reduce 将报错

返回值

函数累计处理的结果

Polyfill

// 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.reduce
if (!Array.prototype.reduce) {
  Object.defineProperty(Array.prototype, 'reduce', {
    value: function (callback /*, initialValue*/) {
      if (this === null) {
        throw new TypeError('Array.prototype.reduce ' +
          'called on null or undefined');
      }
      if (typeof callback !== 'function') {
        throw new TypeError(callback +
          ' is not a function');
      }

      // 1. Let O be ? ToObject(this value).
      var o = Object(this);

      // 2. Let len be ? ToLength(? Get(O, "length")).
      var len = o.length >>> 0;

      // Steps 3, 4, 5, 6, 7      
      var k = 0;
      var value;

      if (arguments.length >= 2) {
        value = arguments[1];
      } else {
        while (k < len && !(k in o)) {
          k++;
        }

        // 3. If len is 0 and initialValue is not present,
        //    throw a TypeError exception.
        if (k >= len) {
          throw new TypeError('Reduce of empty array ' +
            'with no initial value');
        }
        value = o[k++];
      }

      // 8. Repeat, while k < len
      while (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 (k in o) {
          value = callback(value, o[k], k, o);
        }

        // d. Increase k by 1.      
        k++;
      }

      // 9. Return accumulator.
      return value;
    }
  });
}

Reference

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant