Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object.create don't do shallow copies... Does? #32815

Closed
bsshenrique opened this issue Mar 25, 2024 · 1 comment · Fixed by #32829
Closed

Object.create don't do shallow copies... Does? #32815

bsshenrique opened this issue Mar 25, 2024 · 1 comment · Fixed by #32829
Labels
Content:Glossary Glossary entries good first issue A good issue for newcomers to get started with.

Comments

@bsshenrique
Copy link

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 as obj1 = obj2 and we can verify this writing something like that:

const original = {
  value: 'X',
  object: {
    value: 'Y'
  }
};

// Copy by reference
const copy1 = original;
const copy2 = Object.create(original);
// Shallow copy
const copy3 = Object.assign({}, original);

// A function to just help us
function print(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?

No response

Do you have anything more you want to share?

No response

MDN metadata

Page report details
@bsshenrique bsshenrique added the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label Mar 25, 2024
@github-actions github-actions bot added the Content:Glossary Glossary entries label Mar 25, 2024
@Josh-Cena
Copy link
Member

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 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
dawei-wang added a commit to dawei-wang/content that referenced this issue Mar 25, 2024
Remove mention of object.create as a copying technique per mdn#32815 (comment)
Fix mdn#32815
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:Glossary Glossary entries good first issue A good issue for newcomers to get started with.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants