IE8 支持 javascript .map() 函数吗?
执行此操作时:
var a = new Array("a", "b");
a.map(function() { });
在 IE8 中,我得到:
"Object doesn't support this property or method"
IE8 不支持此方法,还是我有其他问题?我有一个 Google,但遇到了很多 Google 地图 javascript 问题/疑问...
编辑:好的,所以 IE8 及以下版本不支持 .map() 函数。从 MDN 复制粘贴代码< /a> 如果本地不支持的话,它将按照规范将 .map() 函数添加到数组原型中(而且它似乎工作得很好)。
When doing this:
var a = new Array("a", "b");
a.map(function() { });
in IE8 I get:
"Object doesn't support this property or method"
Is this method not supported in IE8, or do I have some other problem? I've had a Google, but get lots of Google Maps javascript issues/questions...
Edit: OK so IE8 and below DO NOT support the .map() function. Copy-paste the code from MDN here which will add the .map() function to the Array prototype exactly per the specs if not natively supported (and it seems to work perfectly).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
解决方案是 jQuery.map
而不是这个:
a.map(function( ) { });
你必须做
jQuery.map(a, function( ) {
//你想做什么..
}
The solution is jQuery.map
Instead of this:
a.map(function( ) { });
You have to do
jQuery.map(a, function( ) {
//what ever you want todo ..
}
IE8 不支持
map()
。如有疑问,请检查 MDN(Mozilla 开发者网络):地图 - MDN
看起来 IE 在版本 9 中添加了对
map()
的支持。IE8 doesn't support
map()
. When in doubt, check MDN (Mozilla Developer Network):map - MDN
Looks like IE added support for
map()
in version 9.放在第一个 .map 或 .filter 调用之前的任意位置。问题解决了。 jQuery.map() 方法无法按预期工作。
更新:
我刚刚在稀疏数组上测试了它:如果映射或过滤器参数是一个接受和处理未定义值的函数 - 它可以工作,但结果并不明显:
让我们定义测试稀疏数组:
让我们看看IE8 关于 t: "[undefined, 1, undefined, 3, undefined, 5]"
让我们试试:
这是什么,IE8?它是:“[1, 3]”。注意 - 没有未定义的值。我个人希望如此。
但试试这个:
还有...“[2, 4, 2, 16, 2, 64]”。太奇怪了! :) 试试这个:
然后?... “[NaN,2,NaN,8,NaN,32]” - 我宁愿期待之前测试的结果。它至少是合乎逻辑的 - Math.pow() 应该返回一个
number
类型,NaN
,无论它的含义是一个特殊的number
类型保留用于无效操作。所以结果或多或少是正确的。如果 t 仍然是稀疏数组,那么map
结果将是完全正确的。言归正传 -
map
和filter
方法的最终正确版本:测试:
预期结果:
Put anywhere before first .map or .filter call. Problem solved. jQuery.map() method doesn't work as expected.
UPDATE:
I've just tested it on sparse arrays: if map or filter argument is a function which accepts and handles
undefined
value - it works, but the results are not obvious:Let's define test sparse array:
Let's see what does IE8 say about t: "[undefined, 1, undefined, 3, undefined, 5]"
Let's try:
What is it, IE8? It's: "[1, 3]". Note - no undefined values. I would personally expect that.
But try THIS:
And... "[2, 4, 2, 16, 2, 64]". That's weird! :) Try this:
And?... "[NaN, 2, NaN, 8, NaN, 32]" - I would rather expect this result for the previous test. It's at least logical - Math.pow() is supposed to return a
number
type,NaN
, regardless of it's meaning IS a specialnumber
type reserved for invalid operations. So the result is more or less correct. It would be fully correct asmap
result if t remained a sparse array.So without further ado - ultimately correct version of
map
andfilter
methods:And the test:
Expected results:
MDN 表示 IE 9 支持它。没有提及 IE 8。
MDN says that IE 9 supports it. No mention of IE 8.
MSDN 上的地图要求中提到:
以下文档模式不支持:Quirks、Internet Explorer 6 标准、Internet Explorer 7 标准、Internet Explorer 8 标准。
Map 只是数组“Visitor”模式的实现。它的简单替代品可能是:
该函数还接受数组和在每个数组元素上调用的函数。它返回一个新数组,其中包含每个原始数组元素的访问者调用结果
On MSDN it is said in Requirements for map:
Not supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards.
Map is just a implementation of "Visitor" pattern for array. So easy substitute for it could be:
The function also takes array and function which to invoke on each array element. It returns a new array with result of visitor invokation for each original array element