如其他地方详细说明的那样,以及其他人显然已经知道的,Internet Explorer(特别是版本7,在一些情况下是版本8)没有实现关键函数,尤其是在Array
上(例如forEach
、indexOf
等)。
有许多解决方法,但我想将一个适当的、规范的实现集成到我们的网站中,而不是复制粘贴或对我们自己的实现进行修改。我发现了js-methods,看起来很有前途,但我想在这里发布帖子,看看是否有其他更受推荐的库。还有一些杂项标准:
如其他地方详细说明的那样,以及其他人显然已经知道的,Internet Explorer(特别是版本7,在一些情况下是版本8)没有实现关键函数,尤其是在Array
上(例如forEach
、indexOf
等)。
有许多解决方法,但我想将一个适当的、规范的实现集成到我们的网站中,而不是复制粘贴或对我们自己的实现进行修改。我发现了js-methods,看起来很有前途,但我想在这里发布帖子,看看是否有其他更受推荐的库。还有一些杂项标准:
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方法和一些新的Object
方法,这些方法可以作为JS函数可靠地实现。
了解一下Underscore.js。
Kris Kowal编写了一个小型库,作为ECMAScript 5函数的替代品,以弥补浏览器实现中可能缺失的功能。其中一些函数已经被其他人修订多次,以优化速度并解决浏览器错误。这些函数的编写尽可能地遵循规范。
es5-shim.js是在MIT许可下发布的,Array.prototype扩展位于顶部,您可以轻松地剪切和删除任何不需要的函数。我还建议您最小化脚本,因为注释使其比所需的要大得多。
你所说的“未实现关键函数”实际上是指“符合ECMA 262第3版”,对吗?:)
你提到的方法是新的第5版的一部分 - 对于不支持此功能的浏览器,您可以使用以下“shim”,将第3版扩展到第5版http://github.com/kriskowal/narwhal-lib/blob/narwhal-lib/lib/global-es5.js。
这些脚本在我的测试中效果不佳。我根据 MDN 文档创建了一个具有相同功能的文件。
太多问题区域都已经在 Internet Explorer 8 中得到解决。请参见 egermano / ie-fix.js 中的代码。