javascript Array map with apply
I am trying to declare and initialize an array with 0, in javascript. I
created an array and set the length like this.
var previousRow = [];
previousRow.length = 5;
Then, I did this.
console.log(previousRow);
previousRow = previousRow.map(Number.prototype.valueOf, 0);
console.log(previousRow);
and I got,
[ , , , , ]
[ , , , , ]
But, when I did
console.log(previousRow);
previousRow = Array.apply(null, previousRow).map(Number.prototype.valueOf,
0);
console.log(previousRow);
I got what I expected
[ , , , , ]
[ 0, 0, 0, 0, 0 ]
Why the first code didn't work?
No comments:
Post a Comment