我正在写一些东西,在我的研究中发现数组被记录为具有映射/减少/过滤器,但在 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/