修复Internet Explorer中的JavaScript数组函数(indexOf,forEach等)

如其他地方详细描述的那样,另外显然众所周知的是,Internet Explorer(绝对版本7,以及某些情况下,版本8)不实现关键函数,特别是在Array (例如forEachindexOf等)。

在这里和那里有很多解决方法,但是我想将适当的,规范的实现集合放到我们的站点中,而不是复制,粘贴或者破解我们自己的实现。 我找到了js-methods,看起来很有前途,但是我以为我会在这里发布看看另一个库是否更受推荐。 一些其他标准:

  • 对于浏览器已经实现的那些函数( js-methods在这里表现得很好),这个库应该是一个没有操作的函数。
  • 非GPL,请尽管LGPL是可以接受的。

  • 许多人使用MDC后备实现(例如,用于indexOf)。 它们通常严格符合标准,即使是在明确检查所有参数类型的情况下。

    不幸的是,尽管作者认为这些代码是微不足道且可自由使用的,但似乎并没有明确的许可授权将其写入文档。 整个维基是CC Attribution-ShareAlike,如果这是一个可接受的许可证(尽管CC不是为代码设计的)。

    js-methods在一般情况下看起来不错,但并不像围绕函数应该如何的边缘(例如,未定义的列表项,改变列表的函数)那样符合标准。 这也是充满了其他随机非标准方法,包括一些可疑的像狡猾的stripTags和不完整的UTF-8编码解码器(这也是一个有点不必要给出的unescape(encodeURIComponent)欺骗)。

    对于它的价值,这里是我使用的(如果它可以被认为是可版权的,我在此公开发布到公有领域)。 它比MDC版本更短一点,因为它不尝试键入嗅探,你有没有做过一些愚蠢像传递非函数回调或非整数索引,但除此之外,它试图要符合标准。 (让我知道我是否错过了任何东西;-))

    '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方法包括Array reduce / reduceRight ,JSON方法和少数可以作为JS函数可靠实现的新Object方法。


    看看Underscore.js。


    Kris Kowal编译了一个小型库,作为ECMAScript 5函数的垫片,可能在浏览器的实现中丢失。 其他一些功能已经被其他人修改了很多次,以优化速度并解决浏览器错误。 这些函数的编写要尽可能地遵循规范。

    es5-shim.js是在MIT许可下发布的,Array.prototype扩展接近顶端,您可以轻松地删除和删除不需要的任何函数。 我还建议你尽量缩小脚本,因为评论使它大得多。

    链接地址: http://www.djcxy.com/p/3163.html

    上一篇: Fixing JavaScript Array functions in Internet Explorer (indexOf, forEach, etc.)

    下一篇: How to create a Git alias with nested commands with parameters?