JavaScript: Internet Explorer不支持forEach方法

JavaScript: Method forEach not supported from Internet Explorer

本文关键字:forEach 方法 不支持 Explorer Internet JavaScript      更新时间:2023-09-26

我正在使用gzip算法的javascript实现,该算法与Firefox和Chrome一起工作很好。但是在ie浏览器中,我得到了以下错误:

不支持forEach方法!

代码:

deflate.deflate(data, level).forEach(function (byte) {
    putByte(byte, out);
});

我使用的是Internet Explorer 9,它应该支持forEach方法。

任何想法?

非常感谢!

对于不支持foreach方法的浏览器,您可以尝试扩展Array对象,如Array.forEach

例如:

if (!Array.prototype.forEach) {
    Array.prototype.forEach = function(fn, scope) {
        for(var i = 0, len = this.length; i < len; ++i) {
            fn.call(scope, this[i], i, this);
        }
    }
}

IE9不支持forEach,您可以尝试使用jquery。
例:

$. each (function (byte) {
  putByte(byte, out);
});