JavaScript 的数组过滤方法在 IE 7 中不起作用

An Array's filter method of JavaScript is not working in IE 7

我在 Web 应用程序中使用了一个 jQuery 插件,它在 IE 10 和 11 中运行良好。但它在 IE 7 中失败了。 当我调查时,我才知道 filter 方法的值是 undefined。 失败的代码行如下:

if (splitters.filter(Boolean).length === 0) {

我正在使用jQuery 1.8.3

是JavaScriptfilter() method , it's only supported in IE 9+ as per the MDN documentation


Check polyfill option from MDN for older browser.

if (!Array.prototype.filter) {
  Array.prototype.filter = function(fun/*, thisArg*/) {
    'use strict';

    if (this === void 0 || this === null) {
      throw new TypeError();
    }

    var t = Object(this);
    var len = t.length >>> 0;
    if (typeof fun !== 'function') {
      throw new TypeError();
    }

    var res = [];
    var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
    for (var i = 0; i < len; i++) {
      if (i in t) {
        var val = t[i];

        // NOTE: Technically this should Object.defineProperty at
        //       the next index, as push can be affected by
        //       properties on Object.prototype and Array.prototype.
        //       But that method's new, and collisions should be
        //       rare, so use the more-compatible alternative.
        if (fun.call(thisArg, val, i, t)) {
          res.push(val);
        }
      }
    }

    return res;
  };
}