javascript - 修复 Internet Explorer 中的 JavaScript 数组函数(indexOf、forEach 等)

标签 javascript internet-explorer cross-browser

详细 elsewhere ,以及其他显然众所周知的 Internet Explorer(肯定是版本 7,在某些情况下是版本 8)不实现关键功能,特别是在 Array 上(例如 forEachindexOf 等)。

这里和那里有许多解决方法,但我想将一组适当的、规范的实现合并到我们的网站中,而不是复制和粘贴或修改我们自己的实现。我找到了js-methods ,这看起来很有希望,但我想我会在这里发布,看看是否有另一个库更受高度推荐。一些杂项标准:

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

最佳答案

许多使用 MDC 后备实现(例如 indexOf)。它们通常严格遵守标准,甚至可以明确检查所有参数的类型。

不幸的是,虽然很明显作者认为此代码微不足道且可自由使用,但似乎没有明确的许可授予以书面形式提出。整个 wiki 是 CC Attribution-ShareAlike,如果这是一个可接受的许可(尽管 CC 不是为这样的代码设计的)。

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

对于它的值(value),这就是我使用的东西(我特此将其发布到公共(public)领域,如果它可以说是受版权保护的)。它比 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 方法包括数组 reduce/reduceRight、JSON 方法和少数新的 Object 方法可靠地实现为 JS 函数。

关于javascript - 修复 Internet Explorer 中的 JavaScript 数组函数(indexOf、forEach 等),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2790001/

相关文章:

javascript - Vue.js 无法挂载组件 : template or render function not defined

javascript - 了解范围、ng-click 和 Angular 方式

html - 段落文本在 Safari/iOS 中不换行

html - 在移动浏览器中使用 HTML5 录音(访问麦克风)

javascript - 函数出现意外的未定义返回值

javascript - Semantic-ui 搜索局部静态变量,为什么没有结果?

html - 为什么 "border-top:none"会把我其余的 css 放错地方?

asp.net - Div 背景在 V9 之前的任何 IE 中都不显示

jquery - 是否有一个 jQuery 解决方案,在可用时使用 CORS,并在 MSIE 上使用 XDomainRequest,在浏览器上使用 niether 时使用 JSONP?

html - 如果在 <head> 中执行 document.write,为什么 DHTML 行为在 IE8 中不起作用?