News for hackers. Stuff that matters.
Re:Click by halfmatthalfcat on Friday March 19, 2021 @06:38PM (#26518854)
Can save more bits by doing:

Array.from({ length: 10000 }, button.click);

Re:Click by warent on Friday March 19, 2021 @08:09PM (#26519442)
I this awesome... I've always done Array.from(new Array

But your approach makes perfect sense and is so much cleaner. Thank you!

If anyone is wondering why it works, it's because everything in JavaScript is an object. So, an array is an object with a length prop. Apparently Array.from just needs any object with a length prop to work

Re:Click by kvirani on Friday March 19, 2021 @08:53PM (#26519759)
Something about ducks?
Re:Click by chpmrc on Friday March 19, 2021 @09:32PM (#26519973)
"If it walks like a duck, and it quacks like a duck, then it must be an object"
Re:Click by DonHopkins on Saturday March 20, 2021 @07:17AM (#26522283)
"If it walks like a duck, and it quacks like a duck, then it must be created by a QuackEnumeratorWalkManangerRepositoryProxyFactoryFactoryAdapterBroker." -Java
Re:Click by erhk on Saturday March 20, 2021 @03:13AM (#26521393)
Of it has feathers, then as far as I'm concerned it is a duck.

Similarly, when I pick up a feathered pen in real life I become a duck

Re:Click by chmod775 on Saturday March 20, 2021 @05:17AM (#26521781)
duckduckduck.duck is still available, by the way.

Well, .duck isn't a TLD yet and it may not be publicly available...

https://icannwiki.org/.duck

Re:Click by Freak_NL on Saturday March 20, 2021 @06:19AM (#26521997)
Now gogo.duck and go.duck would be nice aliases for DuckDuckGo. (There is ddg.gg of course.)
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).
Re:Click by a1371 on Friday March 19, 2021 @07:05PM (#26519070)
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

Re:Click by franky47 on Saturday March 20, 2021 @02:06AM (#26521138)
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.

Re:Click by antonvs on Sunday March 21, 2021 @07:38PM (#26535162)
"Harder to grasp" only lasts until you're mildly familiar with the idiom.
Re:Click by trehalose on Saturday March 20, 2021 @07:45PM (#26526943)
[...Array(1e3)]
Re:Click by bawolff on Friday March 19, 2021 @07:01PM (#26519037)
new Array(1000); ?
Re:Click by franky47 on Friday March 19, 2021 @07:05PM (#26519069)
Try iterating on it, it won't work. This creates 1000 "empty slots".
Re:Click by jholman on Friday March 19, 2021 @07:26PM (#26519192)
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
Re:Click by phogster on Friday March 19, 2021 @06:44PM (#26518916)
Never heard of a "from" loop before.
Re:Click by franky47 on Friday March 19, 2021 @06:49PM (#26518963)
Array.from lets you build an array from an iterable source (often used in conjunction with `new Set` to eliminate duplicates), or in this case, of a fixed length.
Re:Click by klyrs on Friday March 19, 2021 @07:40PM (#26519271)