Re:Click
by franky47 on Friday March 19, 2021 @06:46PM
(#26518939)
I did not know this variant, thanks. Having to fill the array with anything (even `undefined`) to iterate on it seemed such a weird concept (but then again, JS is weird).
[ Parent
| on HN ]
The spread operator does not have the same quirk. I use it but it's still a bit weird tough
[...new Array(1000)].map(something)
Re:Click
by ficklepickle on Saturday March 20, 2021 @01:57AM
(#26521094)
You can drop the new
[...Array(1000)]
I think that is the least bits. Would love to be proven wrong :D
It may be shorter, but I like the spread syntax best used sparingly, it's harder to grasp why it was necessary there at a glance.
That's what I like in the elegance of Array.from({ length: 1000 }): it's a hack, but it reads naturally.
"Harder to grasp" only lasts until you're mildly familiar with the idiom.
[...Array(1e3)]
new Array(1000); ?
Try iterating on it, it won't work. This creates 1000 "empty slots".
In addition to franky47's more important point, I'll add that `new Array()` and `Array()` are equivalent.
Re:Click
by The_rationalist on Friday March 19, 2021 @11:10PM
(#26520514)
In java it would be default initialized but it isn't in js