修复 Internet Explorer 中的 JavaScript 数组函数(indexOf、forEach 等)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2790001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fixing JavaScript Array functions in Internet Explorer (indexOf, forEach, etc.)
提问by cemerick
As detailed elsewhere, and otherwise apparently well-known, Internet Explorer (definitely version 7, and in some instances, version 8) do not implement key functions, in particular on Array(such as forEach, indexOf, etc).
至于详细的其他地方,否则显然是众所周知的,IE浏览器(版本绝对7,并在某些情况下,版本8)不落实的关键功能,特别是Array(如forEach,indexOf等)。
There are a number of workarounds here and there, but I'd like to fold a proper, canonical set of implementations into our site rather than copy and paste or hack away at our own implementations. I've found js-methods, which looks promising, but thought I'd post here to see whether another library comes more highly-recommended. A couple of miscellaneous criteria:
这里和那里有许多解决方法,但我想将一组正确的、规范的实现折叠到我们的网站中,而不是复制和粘贴或破解我们自己的实现。我发现了js-methods,它看起来很有希望,但我想我会在这里发帖看看是否另一个库更受欢迎。一些杂项标准:
回答by bobince
Many use the MDC fallback implementations (eg. for indexOf). They're generally rigorously standards-compliant, even to the extent of explicitly checking the types of all the arguments.
许多使用 MDC 回退实现(例如,用于indexOf)。它们通常严格符合标准,甚至可以明确检查所有参数的类型。
Unfortunately whilst it is clear that the authors regard this code as trivial and freely-usable, there doesn't seem to be an explicit licence-grant to put this in writing. The wiki as a whole is CC Attribution-ShareAlike, if that's an acceptable licence (though CC isn't designed for code as such).
不幸的是,虽然很明显作者认为此代码是微不足道的且可自由使用的,但似乎没有明确的许可授权将其写入。维基作为一个整体是 CC Attribution-ShareAlike,如果这是一个可接受的许可证(尽管 CC 不是为代码而设计的)。
js-methods looks OK in general, but is not as standards-compliant around the edges of how the functions are supposed to be (eg. undefined list items, functions that mutate the list). It's also full of other random non-standard methods, including some questionable ones like the dodgy stripTags and the incomplete UTF-8 codec (which is also a bit unnecessary given the unescape(encodeURIComponent)trick).
js-methods 总体上看起来不错,但在函数应该如何(例如,未定义的列表项,改变列表的函数)的边缘不符合标准。它还充满了其他随机的非标准方法,包括一些有问题的方法,例如狡猾的 stripTags 和不完整的 UTF-8 编解码器(考虑到unescape(encodeURIComponent)技巧,这也有点不必要)。
For what it's worth, here's what I use (which I hereby release into the public domain, if it can be said to be copyrightable at all). It's a bit shorter than the MDC versions as it doesn't attempt to type-sniff that you haven't done something silly like pass non-function callbacks or non-integer indexes, but apart from that it attempts to be standards-compliant. (Let me know if I've missed anything. ;-))
对于它的价值,这是我使用的(我特此将其发布到公共领域,如果可以说它完全受版权保护)。它比 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;
};
}
Other ECMA262-5 methods not implemented here include Array reduce/reduceRight, the JSON ones and the few new Objectmethods that can be reliably implemented as JS functions.
此处未实现的其他 ECMA262-5 方法包括 Array reduce/ reduceRight、JSON 方法以及少数Object可以作为 JS 函数可靠实现的新方法。
回答by rfunduk
Take a look at Underscore.js.
回答by Andy E
Kris Kowalhas compiled a small library that acts as a shim for ECMAScript 5 functions that may be missing from the browser's implementation. Some of the functions have been revised numerous times by other people to be optimized for speed and to work around browser bugs. The functions are written to follow the specification as closely as possible.
Kris Kowal编译了一个小型库,它充当浏览器实现中可能缺少的 ECMAScript 5 函数的垫片。有些功能已被其他人多次修改,以优化速度并解决浏览器错误。编写函数以尽可能地遵循规范。
es5-shim.jswas released under the MIT license, the Array.prototype extensions are near the top and you can chop and remove any functions you don't need quite easily. I also suggest you minify the script as the comments make it much larger than it needs to be.
es5-shim.js是在 MIT 许可下发布的,Array.prototype 扩展靠近顶部,您可以很容易地砍掉和删除任何不需要的功能。我还建议您缩小脚本,因为注释使它比需要的大得多。
回答by egermano
Those scripts don't work well in my tests. I create a file with the same functions based on MDNdocuments.
这些脚本在我的测试中效果不佳。我基于MDN文档创建了一个具有相同功能的文件。
Too many problems areas are solved in Internet Explorer 8. See the code in egermano / ie-fix.js.
Internet Explorer 8 中解决了太多问题区域。请参阅egermano / ie-fix.js 中的代码。
回答by Sean Kinsey
By 'not implement key functions' you actually means 'conforms to the ECMA 262 3'rd ed' right? :)
“不实现关键功能”实际上是指“符合 ECMA 262 3'rd ed”,对吗?:)
The methods you are referring to are part of the new 5'th edition - for browsers not supporting this you can use the following 'shim' that extends 3'rd into 5'th http://github.com/kriskowal/narwhal-lib/blob/narwhal-lib/lib/global-es5.js.
您所指的方法是新的第 5 版的一部分 - 对于不支持此功能的浏览器,您可以使用以下“垫片”将 3'rd 扩展到第 5 个 http://github.com/kriskowal/narwhal- lib/blob/narwhal-lib/lib/global-es5.js。
回答by sri_bb
With the Underscore.js
使用 Underscore.js
var arr=['a','a1','b']
_.filter(arr, function(a){ return a.indexOf('a') > -1; })
var arr=['a','a1','b']
_.filter(arr, function(a){ return a.indexOf('a') > -1; })

