Skip to the solution
MediumDSAAsked in 20247 min read

Custom Object.assign polyfill

1.9k views

The problem

Problem, as an interviewer would state it: "Implement Object.assign-equivalent behavior from scratch — merging one or more source objects into a target, matching real native Object.assign's own documented contract, including which object reference it actually returns."

Examples:

Code
Object.assign({a: 1}, {b: 2}, {a: 3, c: 4}); // {a: 3, b: 2, c: 4}

Clarifying questions expected:

  • When multiple sources define the same key, does the LAST source genuinely win, matching a real, predictable left-to-right merge order?
  • Does the target object get MUTATED in place, or is a genuinely new object returned?
  • Should enumerable Symbol-keyed properties on a source be copied too, not just string keys?

Code / implementation expected: Yes — real, direct proof of correct left-to-right merge-and-overwrite order, that the SAME target reference is mutated and returned, and that enumerable Symbol-keyed properties are correctly copied too.

  • js-coding
  • utility
  • algorithm

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

7 min read · 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal, direct proof: the Object.assign polyfill matches real native output, mutates and returns the same target reference, and copies enumerable symbol keys correctly

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 72 of 154 in JavaScript coding

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

Custom getElementsByClassName traverse

Continue