正如详细的其他地方和其他显然众所周知的,Internet (绝对是版本7,在某些情况下,版本8)没有实现关键功能,特别是在Array上(如forEach、indexOf等)。
这里和那里有许多解决办法,但我希望将一组正确的规范实现折叠到我们的站点中,而不是复制、粘贴或攻击我们自己的实现。我已经找到了js-方法,它看上去很有希望,但我想在这里张贴,看看是否有更强烈的推荐另一个库。若干杂项标准:
js-methods在这里似乎做得很好)。发布于 2010-05-07 18:10:38
许多人使用MDC后备实现(例如。( indexOf)。它们通常符合严格的标准,甚至可以显式检查所有参数的类型。
不幸的是,虽然很明显,作者认为这段代码是琐碎的和可自由使用的,但似乎没有明确的许可将其写成书面形式。wiki作为一个整体是CC属性-ShareAlike,如果这是一个可接受的许可(尽管CC不是为代码本身设计的)。
一般情况下,js-方法看起来没问题,但是在函数应该是什么样的边缘时,它并不是标准兼容的(例如。未定义的列表项、更改列表的函数)。它也充满了其他随机的非标准方法,包括一些可疑的方法,如不可靠的stripTags和不完整的UTF-8编解码器(考虑到unescape(encodeURIComponent)技巧,这也有点不必要)。
至于它的价值,这里是我使用的(我在此发布到公共领域,如果可以说它是版权的)。它比MDC版本略短一些,因为它不试图键入嗅探您没有做过一些愚蠢的事情,比如pass非函数回调或非整数索引,但除此之外,它还试图符合标准。(如果我漏掉了什么,请告诉我。;-)
'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方法和少数几个可以作为JS函数可靠实现的新Object方法。
发布于 2010-05-07 18:13:58
发布于 2011-03-10 10:29:49
克里斯·科瓦尔已经编译了一个小型库,它充当浏览器实现中可能缺少的ECMAScript 5函数的垫片。一些功能已经被其他人多次修改,以优化速度,并解决浏览器错误。这些函数是为了尽可能地遵循规范而编写的。
es5-shim.js是在麻省理工学院的许可下发布的,Array.prototype扩展接近顶部,您可以分割和删除任何您不需要的函数。我还建议您缩小脚本,因为注释使它比需要的要大得多。
https://stackoverflow.com/questions/2790001
复制相似问题