对象不支持IE 11中的属性或方法“查找”

时间:2019-05-23 07:18:25

标签: ecmascript-6 babel polyfills

我的Javascript项目的某些find和findIndex方法在IE 11中不起作用。 这里package.json

{
  "name": "form_builder_p1_old",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "babel js -d lib"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/cli": "^7.4.4",
    "@babel/core": "^7.4.5",
    "@babel/plugin-transform-runtime": "^7.4.4",
    "@babel/plugin-transform-spread": "^7.2.2",
    "@babel/preset-env": "^7.4.5"
  },
  "dependencies": {
    "@babel/polyfill": "^7.4.4",
    "@babel/runtime": "^7.4.5",
    "core-js": "^3.0.1",
    "lodash": "^4.17.11",
    "regenerator-runtime": "^0.13.2"
  }
}

和.babelrc

{
  "presets":[
    [
      "@babel/preset-env",
      {
        "useBuiltIns": "entry",
        "corejs": "^3.0.1"
      }
    ]

  ],
  "plugins": ["@babel/plugin-transform-spread"]
}

Babel在IE中工作正常。但是查找方法不起作用。 我尝试过polyfil但没有成功, 这是我的项目目录。

enter image description here

请帮助我。

谢谢

2 个答案:

答案 0 :(得分:1)

我通过为core-js @ 3添加以下行来解决了这个问题。

import "core-js/stable";
import "regenerator-runtime/runtime";

请参阅@ https://babeljs.io/blog/2019/03/19/7.4.0

答案 1 :(得分:0)

我遇到了同样的情况。也许您可以从Array.prototype.find的MDN文档中使用手写的polyfill。

// 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 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 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;
    },
    configurable: true,
    writable: true
  });
}