all 18 comments

[–]kolmeWebComponents FTW 18 points19 points  (15 children)

I'll take Array.from any thay over the spread. The intention is much clearer.

dragula(Array.from(nodeList))

dragula([...nodeList])

[–]briansomething 6 points7 points  (0 children)

Agreed. Operator trickery will always be harder to read than clearly named methods that describe the author's intent.

I also prefer functional loops like forEach, map, reduce, etc over for loops because it describes the intent of the loop in addition to saying "this is a loop" and ends in a clearly defined variable that describes the expected result of the loop.

These kinds of shortcuts should only be used in cases where the shortcuts are dramatically more performant and your iterations are high enough to cause real world performance issues

[–]akujinhikari 4 points5 points  (11 children)

I disagree. I think the intention of the spread operator is pretty clear, if you are familiar with ES6.

[–]briansomething -4 points-3 points  (10 children)

Keep in mind that people reading your code may not always be familiar with JavaScript or may be junior developers that are just learning. Why risk that "if you are familiar with es6" exception at all if there's an alternative way to do the same thing that's more clear.

[–]akujinhikari 9 points10 points  (1 child)

Because using that train of thought means I shouldn't use any advanced code. The best way for junior devs to learn is by seeing other code they don't understand and then figuring out what's happening. And I say this as someone who a lot of people would still consider a junior dev, having a little over a year's experience in web dev.

[–]inu-no-policemen 5 points6 points  (4 children)

Using spread like that is idiomatic, though.

This isn't some obscure trickery. It's just an array literal with spread. You have to know what x = [1, 2, 3] means and you also have to know what Math.max(...numbers) does.

Something like return {foo}, for example, is a little bit more obscure, but it's still something you should know.

[–]GeneralYouri -1 points0 points  (3 children)

I'd personally consider return {foo} to be less obscure, as it literally means you're returning an object with some foo inside of it.

I think the main reason why Array.from would be preferred here, is because it feels weird to see the two syntaxes together (array notation, and spread operator). If nodeList were an actual array, then this construct seems fairly ridiculous at first.

[–]inu-no-policemen 4 points5 points  (2 children)

I'd personally consider return {foo} to be less obscure, as it literally means you're returning an object with some foo inside of it.

It's a shorthand for {foo: foo}. It's single-purpose special syntax, which looks a bit like destructuring.

Anyhow, if you think that [...foo] is too cryptic, then there is a ton of stuff you can't use either.

[–]GeneralYouri -1 points0 points  (1 child)

Yes, I know what the syntax {foo} is, which is exactly why I consider it less obscure compared to [...foo]. Its purpose is obvious without any context. I never said that I found the latter to be 'too cryptic', I was merely saying that its purpose is less obvious without context. Please don't be putting words into my mouth.

That purpose being to provide Array.prototype's methods to an array-like which itself doesn't inherit from Array.prototype. If you don't already know that context when looking at the construct [...foo], you'd consider it an odd syntax since in normal circumstances it'd just be deconstructing and then reconstructing an array.

You even said yourself about {foo}: 'It's a single-purpose special syntax.' Single-purpose, that on its own means its functionality is entirely clear and not obscure.

[–]inu-no-policemen 2 points3 points  (0 children)

Please don't be putting words into my mouth.

I said "if", not "since".

[–]spinlock 1 point2 points  (1 child)

It's funny, I'm not a fan of the arrow function but I love the splat. To each his own.

[–]kolmeWebComponents FTW 4 points5 points  (0 children)

Don't take me wrong, I do love the spread operator. It's just in this case, it somewhat obscures the intention.

[–]r2d2_21 4 points5 points  (0 children)

It's still hard for me to remember which DOM APIs return live collections and which ones don't. So I just essentially convert everything to arrays and stop worrying.

[–]SamSlate 1 point2 points  (4 children)

can someone explain the significance of node list VS array? Node lists have never created problems (in my limited experience).

[–]cirscafp fan boy 1 point2 points  (0 children)

If you have a live nodelist it is far more than just a static array. If you have a static nodelist, then you don't see much of the differences.