Решение проблем совместимости при использовании метода javascript reduce () в IE8 - PullRequest
0 голосов
/ 05 августа 2020

Привет, у меня есть веб-программа, которую мне нужно запустить в IE8. В IE8 метод уменьшения javascript напрямую не поддерживается, поэтому я передал в Polyfill метод уменьшения, как упоминалось здесь: IE / JS: сокращение для объекта

Теперь я У нас возникла другая проблема с Object.defineProperty (), когда объект не поддерживает это действие. Я искал это решение Альтернатива Object.defineProperty для IE8 , но я не могу понять, как передать его в Polyfill в качестве альтернативы Object.defineProperty ().

Ищете методы, как исправить Polyfill, чтобы уменьшить работу и решить проблему Object.defineProperty () или любой другой метод, чтобы получить сокращение, работающее в IE8.

1 Ответ

0 голосов
/ 06 августа 2020

reduce() не поддерживается в старых браузерах, вы можете использовать полифил, как показано ниже. С этим полифилом 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));
...