mercredi 15 avril 2015

Why can I not invoke Array.prototype.map directly on this Array instance?


Vote count:

0




Why can I not invoke Array.prototype.map directly on Array instance o?



var o = Array(3); // [ undefined, undefined, undefined ]
o.map((x,y) => y*2); // [ undefined, undefined, undefined ]


Instead, I have to use apply (or call):



Array.apply(0, o).map((x, y) => y*2)); // [ 0, 2, 4 ]


What am I missing?


Finally, an alternative to the above is:



[...o].map((x, y) => y*2); // [ 0, 2, 4]


I presume because this corrects whatever is missing in my original implementation.



asked 49 secs ago

Ben Aston

8,550






Why can I not invoke Array.prototype.map directly on this Array instance?

Aucun commentaire:

Enregistrer un commentaire