After coming across this particular question, I decided to experiment with modifying the code that generates an array filled with zeroes:
Array.apply(null, Array(10)).map(Number.prototype.valueOf, 0);
Wanting to deepen my understanding of the map
function in JavaScript, I attempted to use fill
instead. However, upon executing the following in Chrome's developer tools, I encountered an error:
Array.apply(null, Array(10)).fill(0).map(Number.prototype.valueOf);
Uncaught TypeError: Number.prototype.valueOf is not generic
My comprehension of the map function leads me to believe that it should execute the callback on each element of the array using that element as its argument. The creation of the array seems to be successful with
Array.apply(null, Array(10)).fill(0)
, so theoretically it should be running Number.prototype.valueOf(0)
. Why then does it result in an error?