【问题标题】:Object doesn't support property or method 'fill'对象不支持属性或方法“填充”
【发布时间】:2020-04-03 19:51:51
【问题描述】:

我在网上找不到同样的问题。 IE 11 给出错误“对象不支持属性或方法fill”。

var arr = new Array(5);
arr.fill(false);

是否有任何方便的方法来填充数组而不是使用for 循环?谢谢。

【问题讨论】:

    标签: javascript


    【解决方案1】:

    我面临同样的问题,不添加任何东西。

    只需打开 polyfills.ts 文件并取消注释以下行:

    /** IE9, IE10 and IE11 requires all of the following polyfills. **/
    import 'core-js/es6/symbol';
    import 'core-js/es6/object';
    import 'core-js/es6/function';
    import 'core-js/es6/parse-int';
    import 'core-js/es6/parse-float';
    import 'core-js/es6/number';
    import 'core-js/es6/math';
    import 'core-js/es6/string';
    import 'core-js/es6/date';
    import 'core-js/es6/array';
    import 'core-js/es6/regexp';
    import 'core-js/es6/map';
    import 'core-js/es6/weak-map';
    import 'core-js/es6/set';
    

    一切都会开始工作。

    【讨论】:

      【解决方案2】:

      安装trivial polyfill 并继续使用.fill(…)

      【讨论】:

      • 这应该是正确的答案。不要为您最喜欢的方法是否可用而烦恼 - 只需提供一个 polyfill 以防万一。
      【解决方案3】:

      您可以使用Array.apply 获取具有所需长度的数组,然后将值映射到它。

      var a = Array.apply(null, { length: 5 }).map(function () { return false; });
      console.log(a);

      【讨论】:

      • 请注意,这对于大型数组来说非常慢。
      【解决方案4】:

      我遇到了同样的问题,需要取消注释 polyfill.ts 文件中的以下行,如下所示:

      Polyfill.ts 文件路径:angular-App=> src=> polyfill.ts

      打开此文件并取消注释以下行

      /** IE9, IE10 and IE11 requires all of the following polyfills. **/
      // import 'core-js/es6/symbol';
      // import 'core-js/es6/object';
      // import 'core-js/es6/function';
      // import 'core-js/es6/parse-int';
      // import 'core-js/es6/parse-float';
      // import 'core-js/es6/number';
      // import 'core-js/es6/math';
      import 'core-js/es6/string';
      // import 'core-js/es6/date';
       import 'core-js/es6/array';
      // import 'core-js/es6/regexp';
      // import 'core-js/es6/map';
      // import 'core-js/es6/weak-map';
      // import 'core-js/es6/set';
      

      现在它对我有用。

      【讨论】:

        【解决方案5】:

        填充();在 IE 中不支持。虽然它是在 EDGE 中引入的。我想 foreach 是完成任务的便捷方式。如果发现更多将更新。您可以阅读以下内容:

        【讨论】:

          【解决方案6】:

          添加以下脚本以将 pollyfill 从 CDN 下载到您的索引文件:

          <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=default,Array.prototype.includes"></script>
          

          这应该可行。

          【讨论】:

            【解决方案7】:

            const arr = Array.from(Array(10), () => 'none')
            console.log(arr)
            // ['none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none']

            【讨论】:

              猜你喜欢
              • 2019-07-05
              • 2015-04-14
              • 2020-09-03
              • 2015-04-09
              • 2014-01-12
              • 2016-08-20
              • 2013-10-05
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多