javascript - 匀场映射/减少/过滤器在 Exploder 6/7/8 中工作吗?

标签 javascript internet-explorer prototype backwards-compatibility shim

我正在写一些东西,在我的研究中发现数组被记录为具有映射/减少/过滤器,但在 IE 中没有?8?或更老。于是我四处寻找,发现JavaScript: What dangers are in extending Array.prototype? ,除了一些回应来冷却我扩展 Array.prototype (甚至可能作为垫片)的热情之外,已接受的回应说:

...This is annoying and has bit me. Old IE sometimes has problems with adding this kind of functionality. You'll just have to see if it works in a case by case basis. For me the problem I had was adding Object.keys to IE7. It seemed to stop working under certain circumstances. Your mileage may vary.

所以,换句话说,我的用例是为旧版 IE 进行填充,这是一种在旧版 IE 中可能不可靠的机制。

所以如果我想支持旧版 IE,我应该制作并使用我自己的独立映射/减少/过滤器吗?我计划在我现在写的内容中采取不同的方法,但为了引用起见,最好知道“如果我想要浏览器 Y 中没有的功能 X,并且垫片可能会在浏览器 Y 中损坏,那么什么是首选方法?”

最佳答案

只要正确完成,我不认为扩展 native 有什么问题:

if(!Array.prototype.map) {
  Array.prototype.map = //your shim
}

这样,您就不会破坏已经存在的 native 方法。

只需确保您的垫片与规范/当前实现保持同步,否则您将遇到一些非常令人困惑的错误。

关于javascript - 匀场映射/减少/过滤器在 Exploder 6/7/8 中工作吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27987112/

相关文章:

javascript - 在 Object.create 中使用属性描述符的正确方法是什么?

javascript - 原型(prototype)未按预期工作

javascript - 类型错误:无法读取 JSON 上未定义的 Ajax 调用的属性 'value'

javascript - 使用 Javascript 获取没有 ID 的 Div 的值(只有一个类)

javascript - 从两个指令访问范围数据

javascript - 下载 PDF 文件不适用于 Angular 和 JavaScript

internet-explorer - 使用 Source Maps 进行 Visual Studio 调试

javascript - 将工作表添加到用户选择的excel文件中

html - IE8 历史记录自动填充位于随机位置

C 原型(prototype)范围