即过滤不受支持

问题描述 投票:-1回答:1

即对我不好。我的jquery代码的一部分有一个.filter(),即发脾气说它的语法正确。

有没有解决的办法?

这条线是

const lines = nar.split("\n").filter(line => line);
javascript internet-explorer
1个回答
1
投票

IE 9 +确实支持过滤器,但它不支持箭头功能或const,你应该用以下内容替换它们:

var lines = nar.split("\n").filter(function (line) { return line; });

你也可以通过使用像babel这样的东西来解决这个问题

但是,如果你的目标IE <9,那么你可能还需要polyfill这个功能

if (!Array.prototype.filter)
  Array.prototype.filter = function(func, thisArg) {
    'use strict';
    if ( ! ((typeof func === 'Function' || typeof func === 'function') && this) )
        throw new TypeError();

    var len = this.length >>> 0,
        res = new Array(len), // preallocate array
        t = this, c = 0, i = -1;
    if (thisArg === undefined)
      while (++i !== len)
        // checks to see if the key was set
        if (i in this)
          if (func(t[i], i, t))
            res[c++] = t[i];
    else
      while (++i !== len)
        // checks to see if the key was set
        if (i in this)
          if (func.call(thisArg, t[i], i, t))
            res[c++] = t[i];

    res.length = c; // shrink down array to proper size
    return res;
  };
© www.soinside.com 2019 - 2024. All rights reserved.