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

后端 未结 14 1895
粉色の甜心
粉色の甜心 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条回答
  •  -上瘾入骨i
    2020-11-22 03:23

    From the MDC page for map:

    [...] callback is invoked only for indexes of the array which have assigned value; [...]

    [undefined] actually applies the setter on the index(es) so that map will iterate, whereas new Array(1) just initializes the index(es) with a default value of undefined so map skips it.

    I believe this is the same for all iteration methods.

提交回复
热议问题