【问题标题】:Object doesn't support property or method 'find' In IE 11对象不支持 IE 11 中的属性或方法“查找”
【发布时间】:2019-05-23 07:18:25
【问题描述】:

我的 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 中运行良好。但是 find 方法不起作用。 我尝试过 polyfil 但没有成功, 这是我的项目目录。

请帮帮我。

谢谢

【问题讨论】:

    标签: ecmascript-6 babeljs polyfills


    【解决方案1】:

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

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

    见@https://babeljs.io/blog/2019/03/19/7.4.0

    另一种解决方案

    手动添加部分模块。

    import 'core-js/modules/es.array.find';
    

    【讨论】:

      【解决方案2】:

      我遇到了同样的情况。也许你可以使用一个手写的 polyfill,来自 Array.prototype.find 的 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 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
        });
      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-02-24
        • 1970-01-01
        • 2016-10-13
        • 2019-07-05
        • 1970-01-01
        • 2020-08-04
        • 1970-01-01
        相关资源
        最近更新 更多