You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What information was incorrect, unhelpful, or incomplete?
"In JavaScript, standard built-in object-copy operations (spread syntax, Array.prototype.concat(), Array.prototype.slice(), Array.from(), Object.assign(), and Object.create()) do not create deep copies (instead, they create shallow copies)."
Object.create() is same as obj1 = obj2 and we can verify this writing something like that:
constoriginal={value: 'X',object: {value: 'Y'}};// Copy by referenceconstcopy1=original;constcopy2=Object.create(original);// Shallow copyconstcopy3=Object.assign({},original);// A function to just help usfunctionprint(o){return`value: ${o.value} | object.value: ${o.object.value}`}original.value='A'original.object.value='B'console.log('\n',`Copy 1: ${print(copy1)}\n`,`Copy 2: ${print(copy2)}\n`,`Copy 3: ${print(copy3)}\n`,)
We'll see something like:
Copy 1: value: A | object.value: B
Copy 2: value: A | object.value: B
Copy 3: value: X | object.value: B
As we can see, when using Object.create() the top level value changes.
To be considered a Shallow Copy instruction this should not be happen, according to this page:
For shallow copies, only the top-level properties are copied, not the values of nested objects. Therefore:
Re-assigning top-level properties of the copy does not affect the source object.
Re-assigning nested object properties of the copy does affect the source object.
What did you expect to see?
"In JavaScript, standard built-in object-copy operations (spread syntax, Array.prototype.concat(), Array.prototype.slice(), Array.from(), Object.assign()) do not create deep copies (instead, they create shallow copies)."
Do you have any supporting links, references, or citations?
Object.create does not create a copy. It creates an object with the passed object as its prototype, which breaks our definition of "copy" by the "structurally equivalent prototype chain" criterion.
I don't know why Object.create is mentioned as a copying technique. Welcoming a PR to remove it.
Josh-Cena
added
good first issue
A good issue for newcomers to get started with.
and removed
needs triage
Triage needed by staff and/or partners. Automatically applied when an issue is opened.
labels
Mar 25, 2024
MDN URL
https://developer.mozilla.org/en-US/docs/Glossary/Deep_copy
What specific section or headline is this issue about?
https://github.com/mdn/content/blob/main/files/en-us/glossary/shallow_copy/index.md?plain=1#L27
What information was incorrect, unhelpful, or incomplete?
"In JavaScript, standard built-in object-copy operations (spread syntax, Array.prototype.concat(), Array.prototype.slice(), Array.from(), Object.assign(), and Object.create()) do not create deep copies (instead, they create shallow copies)."
Object.create()
is same asobj1 = obj2
and we can verify this writing something like that:We'll see something like:
As we can see, when using
Object.create()
the top levelvalue
changes.To be considered a
Shallow Copy
instruction this should not be happen, according to this page:What did you expect to see?
"In JavaScript, standard built-in object-copy operations (spread syntax, Array.prototype.concat(), Array.prototype.slice(), Array.from(), Object.assign()) do not create deep copies (instead, they create shallow copies)."
Do you have any supporting links, references, or citations?
No response
Do you have anything more you want to share?
No response
MDN metadata
Page report details
en-us/glossary/deep_copy
The text was updated successfully, but these errors were encountered: