详细 elsewhere ,以及其他显然众所周知的 Internet Explorer(肯定是版本 7,在某些情况下是版本 8)不实现关键功能,特别是在 Array
上(例如 forEach
、indexOf
等)。
这里和那里有许多解决方法,但我想将一组适当的、规范的实现合并到我们的网站中,而不是复制和粘贴或修改我们自己的实现。我找到了js-methods ,这看起来很有希望,但我想我会在这里发布,看看是否有另一个库更受高度推荐。一些杂项标准:
最佳答案
许多使用 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/