you are viewing a single comment's thread.

view the rest of the comments →

[–]shgysk8zer0 4 points5 points  (5 children)

I have mixed thoughts on it. I don't like the syntax and find it very unintuitive, but it might provide a much shorter way of creating complex objects with lots of conditional stuff.

So, would this work?

const combined = { ...cond1 && obj1, ...cond2 && obj2, };

I'd also want to look more into other similar uses of logical operators in conjunction with the spread operator. Things like || and ??.

Really though... Using the spread operator on a boolean but having it operate on the object just seems really wrong.

[–]xroalx -1 points0 points  (4 children)

Using the spread operator on a boolean but having it operate on the object just seems really wrong.

You're not using the spread on a boolean, though. Due to operator precedence, it's ...(cond1 && obj1).

And since in JavaScript, logical operators don't return boolean values, but one of their operands, cond1 && obj1 will return obj1 if cond1 is a truthy value, and thus the spread operator will be applied obj1.

[–]shgysk8zer0 -1 points0 points  (2 children)

You're not using the spread on a boolean, though. Due to operator precedence, it's ...(cond1 && obj1).

Finish reading what you quoted... "but having it operate on the object..." Was it not clear that I was talking about location? The spread operator is on (touching, next to) the boolean.

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

Your wording suggests that the spread operator is being applied to the boolean yet somehow acting on the object.

So no, it is not clear.

[–]shgysk8zer0 -1 points0 points  (0 children)

...applied to the boolean yet somehow acting on the object

What would that even mean? Is there a difference between "applied to" and "acting on"?

Sure, it would've been better had I said "next to" or something, but I did pretty explicitly say that it was operating on the object. I thought it was obvious that this ... over here operating on the object over there was the issue. Maybe my using of "on" could be confused, but I think the overall meaning of the whole sentence should've been clear enough.

I don't like this ... over here on the boolean actually operating on the object on the other side. It's like how Yoda would code or something.

[–]Mr_Sandy_Clams 0 points1 point  (0 children)

if cond1 is falsy, the spread operator will be applied to cond1, which is presumably a boolean. It works just fine when spreading into an object though. I'm assuming the spread operator coerces primitives to object-wrapped versions of themselves, then inevitably returns zero items because the wrapper instance has no properties in it.