简体   繁体   中英

Solving compatibility issues running javascript reduce() method on IE8

Hi I have a web program that I need to run in IE8. On IE8 the javascript reduce method is not supported directly so I transferred in the Polyfill for the reduce method as mentioned here: IE/JS: reduce on an object

Now I've ran into another issue with Object.defineProperty() where the object doesn't support this action. I've been looking at this solution Object.defineProperty alternative for IE8 but I can't figure out how to transfer it over to the Polyfill as an alternative to Object.defineProperty().

Looking for methods on how to fix up the Polyfill to get reduce working and solve the Object.defineProperty() issue or any other method to get reduce running on IE8.

reduce() isn't supported in old browsers, you can use the polyfill like below. With this polyfill, reduce() can work well in 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;
  };
}

Sample code:

 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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM