简体   繁体   English

对象解构与扩展运算符配对是否会在内存中创建新引用?

[英]Will object destructuring paired with a spread operator create a new reference in memory?

I have a function where I want to omit a property ('uuid') from an object ('readingListObj') without mutating the object itself.我有一个函数,我想在不改变对象本身的情况下从对象 ('readingListObj') 中省略属性 ('uuid')。

Will implementing object destructuring paired with the spread operator on the 'readingListItem' object create a new reference for 'uuid' and 'item'?将对象解构与“readingListItem”对象上的展开运算符配对,是否会为“uuid”和“item”创建新引用?

removeId(readingListObj) {
    const { uuid, ...item } = readingListObj // <---will this line create a new ref for'uuid' and 'item'?

    return item
  }

The simplest approach would be to just check最简单的方法就是检查

 const object1 = {a: 1, b: 2, c: {c: 1}}; const {a, ...object2} = object1; // try to modify basic value property object2.b = 3; console.log(object2.b, object1.b); // returns different values meaning that objects are indeed different // try to modify object value property object2.cc = 2 console.log(object2.cc, object1.cc) // returns the same values

The second modification returning same values means that even though destructurizing created a new object it didn't do a deep copy of it, so there is still a possibility of mutation of the original object.返回相同值的第二个修改意味着即使解构创建了一个新对象,它也没有对其进行深拷贝,因此仍然存在原始对象发生变异的可能性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM