【问题标题】:Find and Arrow notations not working in IE查找和箭头符号在 IE 中不起作用
【发布时间】:2017-10-11 09:19:34
【问题描述】:

这是我的小提琴https://jsfiddle.net/y1s6pttt/,我在其中写下了我的代码

这适用于 Chrome 和 Mozilla,但不适用于 IE。问题在于箭头符号。箭头符号在 IE 中不起作用。

这是我在 IE 中遇到问题的代码部分。

  months1 = data.reduce((p,c) => ~p.indexOf(c.months) ? p : p.concat(c.months),[]),

  series = data.reduce((p,c) => { var f = p.find(f => f.name == c.project_title);

            !!f ? f.data[months1.indexOf(c.months)] = c.amount*1

            : p.push({name: c.project_title, id:c.project_title,

            data: (new Array(months1.length)).fill(0).map((e,i) => i === months1.indexOf(c.months) ? c.amount*1 : e)});

            return p;

         },[]);

我在Babel中执行后将代码替换为下面的代码

  months1 = data.reduce(function (p, c) {

   return ~p.indexOf(c.months) ? p : p.concat(c.months);
    }, []),
    series = data.reduce(function (p, c) {

    var f = p.find(function (f) {

      return f.name == c.project_title;

     });

    !!f ? f.data[months1.indexOf(c.months)] = c.amount * 1 : p.push({ name: c.project_title, id: c.project_title,

    data: new Array(months1.length).fill(0).map(function (e, i) {

    return i === months1.indexOf(c.months) ? c.amount * 1 : e;

   }) });

   return p;
   }, []);

即使用 Babel 代码替换后,我也收到错误 Object 不支持 jquery 中的属性或方法“find”

我需要用任何其他函数替换箭头符号以获得类似的输出。如何根据需要更改代码。

【问题讨论】:

  • 什么版本的ie?

标签: javascript jquery internet-explorer arrow-functions


【解决方案1】:

Internet Explorer 不支持 arrow functions 也不支持数组在其任何版本中具有的 find 函数(这些天我更惊讶 IE 确实 与什么时候相比但它没有)。看起来您已经在 Babel 生成的代码中处理了箭头函数问题,所以现在您只需要为 find 函数添加一个 polyfill。这是我在上面链接到的 MDN 页面中的一个:

// https://tc39.github.io/ecma262/#sec-array.prototype.find
if (!Array.prototype.find) {
  Object.defineProperty(Array.prototype, 'find', {
    value: function(predicate) {
     // 1. Let O be ? ToObject(this value).
      if (this == null) {
        throw new TypeError('"this" is null or not defined');
      }

      var o = Object(this);

      // 2. Let len be ? ToLength(? Get(O, "length")).
      var len = o.length >>> 0;

      // 3. If IsCallable(predicate) is false, throw a TypeError exception.
      if (typeof predicate !== 'function') {
        throw new TypeError('predicate must be a function');
      }

      // 4. If thisArg was supplied, let T be thisArg; else let T be undefined.
      var thisArg = arguments[1];

      // 5. Let k be 0.
      var k = 0;

      // 6. Repeat, while k < len
      while (k < len) {
        // a. Let Pk be ! ToString(k).
        // b. Let kValue be ? Get(O, Pk).
        // c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
        // d. If testResult is true, return kValue.
        var kValue = o[k];
        if (predicate.call(thisArg, kValue, k, o)) {
          return kValue;
        }
        // e. Increase k by 1.
        k++;
      }

      // 7. Return undefined.
      return undefined;
    }
  });
}

如果 IE 版本太旧以至于不支持Object.defineProperty,您可以创建一个全局find 函数,将数组作为参数以及要查找的对象。不过,您需要将所有 this 引用更改为数组对象参数名称。

看到reduce 工作正常,你不应该需要 polyfill 或 indexOf,但我会在此处链接到他们的 polyfill,以防将来的访问者需要它们(它们在 IE

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-06
    • 2012-04-11
    • 2015-04-28
    • 1970-01-01
    • 1970-01-01
    • 2020-05-20
    相关资源
    最近更新 更多