What is the correct fix?
function clone(obj) {
const copy = obj;
copy.x = 99;
return copy;
}
- Change
copy.xtoobj.x - Use
const copy = { ...obj } - Return
objinstead - Use
Object.keys(obj)
Assigning shares the reference; spread to make a real copy.
Play Problem Solving ▸