修复 Internet Explorer 中的 JavaScript 数组函数(indexOf、forEach 等) [英] Fixing JavaScript Array functions in Internet Explorer (indexOf, forEach, etc.)

查看:17
本文介绍了修复 Internet Explorer 中的 JavaScript 数组函数(indexOf、forEach 等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

详细其他地方,以及其他明显众所周知的 Internet Explorer(绝对是第 7 版,在某些情况下是第 8 版) 不实现关键函数,尤其是在 Array 上(例如 forEachindexOf 等).

As detailed elsewhere, and otherwise apparently well-known, Internet Explorer (definitely version 7, and in some instances, version 8) do not implement key functions, in particular on Array (such as forEach, indexOf, etc).

这里和那里有许多变通方法,但我想将一组适当的、规范的实现折叠到我们的网站中,而不是复制和粘贴或破解我们自己的实现.我发现了 js-methods,看起来很有希望,但我想在此处发布以查看是否更推荐另一个库.一些杂项标准:

There are a number of workarounds here and there, but I'd like to fold a proper, canonical set of implementations into our site rather than copy and paste or hack away at our own implementations. I've found js-methods, which looks promising, but thought I'd post here to see whether another library comes more highly-recommended. A couple of miscellaneous criteria:

  • 对于浏览器已经实现的那些函数,该库应该只是一个无操作(js-methods 在这里似乎做得很好).
  • GPL,尽管LGPL 是可以接受的.
  • The library should just be a no-operation for those functions that a browser already has implementations for (js-methods appears to do quite well here).
  • Non-GPL, please, though LGPL is acceptable.

推荐答案

许多使用 MDC 回退实现(例如,对于 indexOf).它们通常严格符合标准,甚至到了明确检查所有参数类型的程度.

Many use the MDC fallback implementations (eg. for indexOf). They're generally rigorously standards-compliant, even to the extent of explicitly checking the types of all the arguments.

不幸的是,虽然作者显然认为此代码微不足道且可自由使用,但似乎没有明确的许可授权将其写入.整个 wiki 是 CC Attribution-ShareAlike,如果这是一个可接受的许可证(尽管 CC 不是为这样的代码设计的).

Unfortunately whilst it is clear that the authors regard this code as trivial and freely-usable, there doesn't seem to be an explicit licence-grant to put this in writing. The wiki as a whole is CC Attribution-ShareAlike, if that's an acceptable licence (though CC isn't designed for code as such).

js-methods 总体上看起来不错,但在函数应该如何(例如,未定义的列表项,改变列表的函数)的边缘不符合标准.它还充满了其他随机的非标准方法,包括一些有问题的方法,例如狡猾的 stripTags 和不完整的 UTF-8 编解码器(鉴于 <​​code>unescape(encodeURIComponent) 技巧,这也有点不必要).

js-methods looks OK in general, but is not as standards-compliant around the edges of how the functions are supposed to be (eg. undefined list items, functions that mutate the list). It's also full of other random non-standard methods, including some questionable ones like the dodgy stripTags and the incomplete UTF-8 codec (which is also a bit unnecessary given the unescape(encodeURIComponent) trick).

就其价值而言,这是我使用的(如果可以说它完全受版权保护,我特此将其发布到公共领域).它比 MDC 版本短一点,因为它不会尝试键入嗅探您没有做一些愚蠢的事情,例如传递非函数回调或非整数索引,但除此之外,它试图符合标准.(如果我错过了什么,请告诉我.;-))

For what it's worth, here's what I use (which I hereby release into the public domain, if it can be said to be copyrightable at all). It's a bit shorter than the MDC versions as it doesn't attempt to type-sniff that you haven't done something silly like pass non-function callbacks or non-integer indexes, but apart from that it attempts to be standards-compliant. (Let me know if I've missed anything. ;-))

'use strict';

// Add ECMA262-5 method binding if not supported natively
//
if (!('bind' in Function.prototype)) {
    Function.prototype.bind= function(owner) {
        var that= this;
        if (arguments.length<=1) {
            return function() {
                return that.apply(owner, arguments);
            };
        } else {
            var args= Array.prototype.slice.call(arguments, 1);
            return function() {
                return that.apply(owner, arguments.length===0? args : args.concat(Array.prototype.slice.call(arguments)));
            };
        }
    };
}

// Add ECMA262-5 string trim if not supported natively
//
if (!('trim' in String.prototype)) {
    String.prototype.trim= function() {
        return this.replace(/^s+/, '').replace(/s+$/, '');
    };
}

// Add ECMA262-5 Array methods if not supported natively
//
if (!('indexOf' in Array.prototype)) {
    Array.prototype.indexOf= function(find, i /*opt*/) {
        if (i===undefined) i= 0;
        if (i<0) i+= this.length;
        if (i<0) i= 0;
        for (var n= this.length; i<n; i++)
            if (i in this && this[i]===find)
                return i;
        return -1;
    };
}
if (!('lastIndexOf' in Array.prototype)) {
    Array.prototype.lastIndexOf= function(find, i /*opt*/) {
        if (i===undefined) i= this.length-1;
        if (i<0) i+= this.length;
        if (i>this.length-1) i= this.length-1;
        for (i++; i-->0;) /* i++ because from-argument is sadly inclusive */
            if (i in this && this[i]===find)
                return i;
        return -1;
    };
}
if (!('forEach' in Array.prototype)) {
    Array.prototype.forEach= function(action, that /*opt*/) {
        for (var i= 0, n= this.length; i<n; i++)
            if (i in this)
                action.call(that, this[i], i, this);
    };
}
if (!('map' in Array.prototype)) {
    Array.prototype.map= function(mapper, that /*opt*/) {
        var other= new Array(this.length);
        for (var i= 0, n= this.length; i<n; i++)
            if (i in this)
                other[i]= mapper.call(that, this[i], i, this);
        return other;
    };
}
if (!('filter' in Array.prototype)) {
    Array.prototype.filter= function(filter, that /*opt*/) {
        var other= [], v;
        for (var i=0, n= this.length; i<n; i++)
            if (i in this && filter.call(that, v= this[i], i, this))
                other.push(v);
        return other;
    };
}
if (!('every' in Array.prototype)) {
    Array.prototype.every= function(tester, that /*opt*/) {
        for (var i= 0, n= this.length; i<n; i++)
            if (i in this && !tester.call(that, this[i], i, this))
                return false;
        return true;
    };
}
if (!('some' in Array.prototype)) {
    Array.prototype.some= function(tester, that /*opt*/) {
        for (var i= 0, n= this.length; i<n; i++)
            if (i in this && tester.call(that, this[i], i, this))
                return true;
        return false;
    };
}

此处未实现的其他 ECMA262-5 方法包括数组 reduce/reduceRight、JSON 方法和少数新的 Object 方法可靠地实现为 JS 函数.

Other ECMA262-5 methods not implemented here include Array reduce/reduceRight, the JSON ones and the few new Object methods that can be reliably implemented as JS functions.

这篇关于修复 Internet Explorer 中的 JavaScript 数组函数(indexOf、forEach 等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆