r/learnjavascript • • Feb 16 '21

Javascript Object Merge

Post image
183 Upvotes

28 comments sorted by

View all comments

1

u/[deleted] Feb 17 '21

Nice, my only criticism is if you are at all concerned with prototypes. Hopefully we're in the land of functional patterns by now, but lots of projects still use OOP.

Just add one part to that merge function...

const protoA = Object.getPrototypeOf(a);
const protoB = Object.getPrototypeOf(b);
const proto = { ...protoA, ...protoB };
const obj = Object.create(proto, a);

...or something like that. Forgive me, it's late and I'm not testing this atm. If someone else wants to expand on this feel free, I'm sure it would only be helpful!

1

u/backtickbot Feb 17 '21

Fixed formatting.

Hello, Strange_Night2150: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.