【问题标题】:Solving compatibility issues running javascript reduce() method on IE8解决在 IE8 上运行 javascript reduce() 方法的兼容性问题
【发布时间】:2020-11-25 04:05:48
【问题描述】:

您好,我有一个需要在 IE8 中运行的网络程序。在 IE8 上,不直接支持 javascript reduce 方法,因此我在 Polyfill 中转移了 reduce 方法,如下所述:IE/JS: reduce on an object

现在我遇到了 Object.defineProperty() 的另一个问题,其中对象不支持此操作。我一直在看这个解决方案Object.defineProperty alternative for IE8 但我不知道如何将它转移到 Polyfill 作为 Object.defineProperty() 的替代方法。

寻找有关如何修复 Polyfill 以使 reduce 工作并解决 Object.defineProperty() 问题或任何其他方法以在 IE8 上运行 reduce 的方法。

【问题讨论】:

  • 该死的家伙,我为你感到难过。
  • 嗨@olliejjc16,这个问题怎么样? my answer below 对解决您的问题有帮助吗?如果是这样,您可以参考this,它可以帮助其他社区成员将来解决类似的问题。感谢您的理解。

标签: javascript internet-explorer cross-browser internet-explorer-8 polyfills


【解决方案1】:

reduce() 在旧版浏览器中不受支持,您可以使用如下所示的 polyfill。使用这个 polyfill,reduce() 可以在 IE 8 中正常工作:

if ('function' !== typeof Array.prototype.reduce) {
  Array.prototype.reduce = function(callback, opt_initialValue){
    'use strict';
    if (null === this || 'undefined' === typeof this) {
      // At the moment all modern browsers, that support strict mode, have
      // native implementation of Array.prototype.reduce. For instance, IE8
      // does not support strict mode, so this check is actually useless.
      throw new TypeError(
          'Array.prototype.reduce called on null or undefined');
    }
    if ('function' !== typeof callback) {
      throw new TypeError(callback + ' is not a function');
    }
    var index = 0, length = this.length >>> 0, value, isValueSet = false;
    if (1 < arguments.length) {
      value = opt_initialValue;
      isValueSet = true;
    }
    for ( ; length > index; ++index) {
      if (!this.hasOwnProperty(index)) continue;
      if (isValueSet) {
        value = callback(value, this[index], index, this);
      } else {
        value = this[index];
        isValueSet = true;
      }
    }
    if (!isValueSet) {
      throw new TypeError('Reduce of empty array with no initial value');
    }
    return value;
  };
}

示例代码:

if ('function' !== typeof Array.prototype.reduce) {
  Array.prototype.reduce = function(callback, opt_initialValue) {
    'use strict';
    if (null === this || 'undefined' === typeof this) {
      // At the moment all modern browsers, that support strict mode, have
      // native implementation of Array.prototype.reduce. For instance, IE8
      // does not support strict mode, so this check is actually useless.
      throw new TypeError(
        'Array.prototype.reduce called on null or undefined');
    }
    if ('function' !== typeof callback) {
      throw new TypeError(callback + ' is not a function');
    }
    var index = 0,
      length = this.length >>> 0,
      value, isValueSet = false;
    if (1 < arguments.length) {
      value = opt_initialValue;
      isValueSet = true;
    }
    for (; length > index; ++index) {
      if (!this.hasOwnProperty(index)) continue;
      if (isValueSet) {
        value = callback(value, this[index], index, this);
      } else {
        value = this[index];
        isValueSet = true;
      }
    }
    if (!isValueSet) {
      throw new TypeError('Reduce of empty array with no initial value');
    }
    return value;
  };
}



var array1 = [1, 2, 3, 4];
var reducer = function reducer(accumulator, currentValue) {
  return accumulator + currentValue;
}; // 1 + 2 + 3 + 4
console.log(array1.reduce(reducer)); // expected output: 10
// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));

【讨论】:

    猜你喜欢
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-27
    • 1970-01-01
    • 2014-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多