JavaScript的"新的Array(N)QUOT;和" Array.prototype.map"怪事 [英] JavaScript "new Array(n)" and "Array.prototype.map" weirdness

查看:166
本文介绍了JavaScript的"新的Array(N)QUOT;和" Array.prototype.map"怪事的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我观察这个在Firefox-3.5.7 / Firebug的-1.5.3和Firefox-3.6.16 / Firebug的-1.6.2

I've observed this in Firefox-3.5.7/Firebug-1.5.3 and Firefox-3.6.16/Firebug-1.6.2

当我火了Firebug的:

When I fire up Firebug:

    >>> x = new Array(3)
    [undefined, undefined, undefined]
    >>> y = [undefined, undefined, undefined]
    [undefined, undefined, undefined]

    >>> x.constructor == y.constructor
    true

    >>> x.map(function(){ return 0; })
    [undefined, undefined, undefined]
    >>> y.map(function(){ return 0; })
    [0, 0, 0]

这是怎么回事?这是一个bug,还是我误解了如何使用新阵列(3)

推荐答案

看来,第一个例子

x = new Array(3);

创建具有不确定的指针数组。

Creates an array with undefined pointers.

和所述第二创建具有指针3未定义对象的数组,在这种情况下,指针他们自不未定义,只有对象它们指向

And the second creates an array with pointers to 3 undefined objects, in this case the pointers them self are NOT undefined, only the objects they point to.

y = [undefined, undefined, undefined]

正如图在数组中的对象的上下文中运行相信第一地图失败而第二管理运行在所有运行功能

As map is run in the context of the objects in the array I believe the first map fails to run the function at all while the second manages to run.

这篇关于JavaScript的"新的Array(N)QUOT;和" Array.prototype.map"怪事的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆