【问题标题】:Array.prototype.find() vs IE11Array.prototype.find() 与 IE11
【发布时间】:2019-03-13 17:53:01
【问题描述】:

https://caniuse.com/#search=find 声明 IE11 不支持 find() 方法。

同时我在 IE11 中测试这个 find() 方法,我没有发现任何错误行为的痕迹。

我也在IE11中测试过代码

function isPrime(element, index, array) {
  var start = 2;
  while (start <= Math.sqrt(element)) {
    if (element % start++ < 1) return false;
  }
  return (element > 1);
}

console.log([4, 5, 8, 12].find(isPrime)); // 5

来自 SO: Array.prototype.find() is undefined

是的,在 IE11 中它返回 5 而不是 TypeError: undefined is not a function 的预期结果,正如 2014 年的 SO: Array.prototype.find() is undefined 所述。

所以...我是否遗漏了什么,IE11 真的不能与 Array.prototype.find 一起正常工作,或者是不久前对 IE11 进行的最后更新(但晚于 2014 年讨论的上述 SO 问题) 变成支持这种方法了吗?

当说 IE11 不支持 Array.prototype.find 时,https://caniuse.com/#search=find 是否正确?有证据吗?

谢谢。

UPD:这是我的 IE11 的屏幕:

【问题讨论】:

  • 您是在 SO 的上下文中运行它吗?好吧,SO 可能已经使用 polyfill 定义了该方法……在 "empty" 页面上再试一次!
  • 不,它来自另一个网站。 SO 背景只是另一个(但不是第三个)窗口。这可能是查找该方法是否在我进行测试的网站上定义的原因。我会让你知道。是的 - 我在 SO 中的运行代码 sn-p 中看到它会导致 IE11 出现错误。

标签: javascript cross-browser internet-explorer-11


【解决方案1】:

您阅读的所有内容都是正确的。您的测试存在一些缺陷。也许您包含了一个在 IE11 中添加该方法的 Polyfill。

【讨论】:

    【解决方案2】:

    您可以尝试以下步骤进行验证:

    1. 在 IE 中打开一个空白选项卡。
    2. 在开发工具中打开控制台。
    3. 输入以下代码:[1,2,3].find(function(n) { !!n; })
      • 如果上面的代码抛出错误(应该是这样),那么您使用的是 polyfill。因此您的代码不会中断。
      • 如果它有效,唯一的解释是不知何故,某些更新添加了它的定义。但这不太可能,因为 MS 已停止支持它。

    这是我得到的:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-31
      • 1970-01-01
      • 2018-04-07
      • 1970-01-01
      • 1970-01-01
      • 2021-04-25
      • 1970-01-01
      • 2019-08-18
      相关资源
      最近更新 更多