IE8 支持 javascript .map() 函数吗?

发布于 2024-12-03 09:02:44 字数 518 浏览 1 评论 0原文

执行此操作时:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

御弟哥哥 2024-12-10 09:02:44

解决方案是 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 ..
}

蓝礼 2024-12-10 09:02:44

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.

白色秋天 2024-12-10 09:02:44
(function(fn){
    if (!fn.map) fn.map=function(f){var r=[];for(var i=0;i<this.length;i++)r.push(f(this[i]));return r}
    if (!fn.filter) fn.filter=function(f){var r=[];for(var i=0;i<this.length;i++)if(f(this[i]))r.push(this[i]);return r}
})(Array.prototype);

放在第一个 .map 或 .filter 调用之前的任意位置。问题解决了。 jQuery.map() 方法无法按预期工作。

更新:
我刚刚在稀疏数组上测试了它:如果映射或过滤器参数是一个接受和处理未定义值的函数 - 它可以工作,但结果并不明显:

让我们定义测试稀疏数组:

var t = []
t[1] = 1; t[3] = 3; t[5] = 5;

让我们看看IE8 关于 t: "[undefined, 1, undefined, 3, undefined, 5]"

让我们试试:

t.filter(function(x){return x<4})

这是什么,IE8?它是:“[1, 3]”。注意 - 没有未定义的值。我个人希望如此。

但试试这个:

t.map(function(x){return 2<<x})

还有...“[2, 4, 2, 16, 2, 64]”。太奇怪了! :) 试试这个:

t.map(function(x){return Math.pow(2,x)})

然后?... “[NaN,2,NaN,8,NaN,32]” - 我宁愿期待之前测试的结果。它至少是合乎逻辑的 - Math.pow() 应该返回一个 number 类型,NaN,无论它的含义是一个特殊的 number 类型保留用于无效操作。所以结果或多或少是正确的。如果 t 仍然是稀疏数组,那么 map 结果将是完全正确的。

言归正传 - mapfilter 方法的最终正确版本

(function(fn){
    if (!fn.map) fn.map=function(f){var r=[];for(var i=0;i<this.length;i++)if(this[i]!==undefined)r[i]=f(this[i]);return r}
    if (!fn.filter) fn.filter=function(f){var r=[];for(var i=0;i<this.length;i++)if(this[i]!==undefined&&f(this[i]))r[i]=this[i];return r}
})(Array.prototype);

测试:

var t = []; t[1] = 1; t[3] = 3; t[5] = 5;
var t1 = t.map(function(x){return 2<<x});
var t2 = t.filter(function(x){return x<10});
console.debug(t);
console.debug(t1);
console.debug(t2);

预期结果:

[对象数组][未定义,1,未定义,3,未定义,5]

[对象数组][未定义,4,未定义,16,未定义,64]

[对象数组][未定义,1,未定义,3,未定义,5]

(function(fn){
    if (!fn.map) fn.map=function(f){var r=[];for(var i=0;i<this.length;i++)r.push(f(this[i]));return r}
    if (!fn.filter) fn.filter=function(f){var r=[];for(var i=0;i<this.length;i++)if(f(this[i]))r.push(this[i]);return r}
})(Array.prototype);

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:

var t = []
t[1] = 1; t[3] = 3; t[5] = 5;

Let's see what does IE8 say about t: "[undefined, 1, undefined, 3, undefined, 5]"

Let's try:

t.filter(function(x){return x<4})

What is it, IE8? It's: "[1, 3]". Note - no undefined values. I would personally expect that.

But try THIS:

t.map(function(x){return 2<<x})

And... "[2, 4, 2, 16, 2, 64]". That's weird! :) Try this:

t.map(function(x){return Math.pow(2,x)})

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 special number type reserved for invalid operations. So the result is more or less correct. It would be fully correct as map result if t remained a sparse array.

So without further ado - ultimately correct version of map and filter methods:

(function(fn){
    if (!fn.map) fn.map=function(f){var r=[];for(var i=0;i<this.length;i++)if(this[i]!==undefined)r[i]=f(this[i]);return r}
    if (!fn.filter) fn.filter=function(f){var r=[];for(var i=0;i<this.length;i++)if(this[i]!==undefined&&f(this[i]))r[i]=this[i];return r}
})(Array.prototype);

And the test:

var t = []; t[1] = 1; t[3] = 3; t[5] = 5;
var t1 = t.map(function(x){return 2<<x});
var t2 = t.filter(function(x){return x<10});
console.debug(t);
console.debug(t1);
console.debug(t2);

Expected results:

[object Array] [undefined, 1, undefined, 3, undefined, 5]

[object Array][undefined, 4, undefined, 16, undefined, 64]

[object Array][undefined, 1, undefined, 3, undefined, 5]

萌梦深 2024-12-10 09:02:44

MDN 表示 IE 9 支持它。没有提及 IE 8。

在此处输入图像描述

MDN says that IE 9 supports it. No mention of IE 8.

enter image description here

绝不放开 2024-12-10 09:02:44

MSDN 上的地图要求中提到:
以下文档模式不支持:Quirks、Internet Explorer 6 标准、Internet Explorer 7 标准、Internet Explorer 8 标准。

Map 只是数组“Visitor”模式的实现。它的简单替代品可能是:

function visitArray(arr, visitor) {
    var result = [];

    for (var i = 0; i < arr.length; i ++) {
        result[i] = visitor(arr[i]);
    }

    return result;
}

该函数还接受数组和在每个数组元素上调用的函数。它返回一个新数组,其中包含每个原始数组元素的访问者调用结果

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:

function visitArray(arr, visitor) {
    var result = [];

    for (var i = 0; i < arr.length; i ++) {
        result[i] = visitor(arr[i]);
    }

    return result;
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文