[英]Update object with destructuring // reference
i want update this object(newBook) with destructuring我想用解构更新这个对象(newBook)
let newBook ={
bookId:book._id,
favorite:false,
}
let {favorite} = newBook
favorite = true
favorite need to be a reference newBook.favorite最喜欢的需要参考 newBook.favorite
i see we can update the original objet like that我看到我们可以像这样更新原始对象
let b = newBook
b.favorite = true
but with the destructuring not work, any idea?但是解构不起作用,有什么想法吗?
Reassigning a variable, by itself, never has any side effects (outside of extremely unusual situations like with arguments
and exported ES6 module bindings).重新分配变量本身不会有任何副作用(除了极不寻常的情况,例如
arguments
和导出的 ES6 模块绑定)。 If you have如果你有
someIdentifier = someExpression
that line alone won't change anything else about the code, except where someIdentifier
is used later.仅此行不会更改有关代码的任何其他内容,除非稍后使用
someIdentifier
。
If you want to update the original object, the only real way is to assign to a property of the object, like you're doing with如果要更新原始 object,唯一真正的方法是分配给 object 的属性,就像你正在做的那样
b.favorite = true
or或者
newBook.favorite = true
This doesn't really have anything to do with destructuring.这实际上与解构无关。 Assigning a property of an object to a new variable just doesn't carry over the reference from the parent object to the new variable.
将 object 的属性分配给新变量不会将父 object 的引用传递给新变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.