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

后端 未结 14 1913
粉色の甜心
粉色の甜心 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:39

    It appears that the first example

    x = new Array(3);
    

    Creates an array with undefined pointers.

    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]
    // The following is not equivalent to the above, it's the same as new Array(3)
    y = [,,,];
    

    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.

提交回复
热议问题