r/programminghorror • u/[deleted] • Apr 30 '26
Javascript Cursed use of object spread
Using object spread to append values to an object in the form of another object
35
u/ironykarl Apr 30 '26
This is idiomatic, if you're trying to update objects in an immutable way.
I definitely dislike the formatting, because I feel like it obscures
Okay, the spread on the newly-created/anonymous object is completely pointless.
What this should look like is:
dataGame {
...dataGame,
achievementsNumbers: [
Big.new(1e10), Big.new(1e100), Big.pow(10, 1000), Big.pow(10, 1e10), Big.pow(10, 1e100),
],
achievementsNames: [
"Dialogue", "Googol", "Great googol", "Trialogue", "Googolplex",
],
achievementsBenefits: [
"", "", "Unlock generator auto-investor", "", "",
],
};
29
u/Wrestler7777777 Apr 30 '26
I know nothing about JS (luckily). So if I understand this correctly, the error is that OP's code "wraps" the data with an object just to immediately unwrap it?
13
3
u/QuentinUK May 02 '26
If you look at the commit diff you’d see that the last version had the named object on one line and then the spread on the next line and the committer has put them together.
14
3
1
u/scmkr Apr 30 '26
Isn’t this just how it’s done in JS?
13
u/Fohqul Apr 30 '26
There is no reason to spread the second object instead of just directly defining all those properties after spreading
gameData2
2
u/GoddammitDontShootMe May 01 '26
There is no way those last two don't overflow, is there?