$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · medium · #93

Which fix removes the subtle bug?

const merged = Object.assign({}, base);
merged.opts.debug = true;
console.log(base.opts.debug);
  1. Copy deeply with structuredClone(base)
  2. Copy deeply with Object.entries(base)
  3. Call Object.assign with two sources
  4. Freeze base before the assignment
Object.assign copies one level, so opts is shared and the write leaks into base.
Play Problem Solving ▸