JavaScript “new Array(n)” and “Array.prototype.map” weirdness

后端 未结 14 1911
粉色の甜心
粉色の甜心 2020-11-22 02:40

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

When I fire up Firebug:

14条回答
  •  悲哀的现实
    2020-11-22 03:33

    I think the best way to explain this is to look at the way that Chrome handles it.

    >>> x = new Array(3)
    []
    >>> x.length
    3
    

    So what is actually happening is that new Array() is returning an empty array that has a length of 3, but no values. Therefore, when you run x.map on a technically empty array, there is nothing to be set.

    Firefox just 'fills in' those empty slots with undefined even though it has no values.

    I don't think this is explicitly a bug, just a poor way of representing what is going on. I suppose Chrome's is "more correct" because it shows that there isn't actually anything in the array.

提交回复
热议问题