Object.assign problem

looking at the Vue official website tutorial, there are the following suggestions for adding multiple attributes:

= split line =
if you want to add new responsive attributes, don"t be like this:

Object.assign ( vm.userProfile , {
age: 27,
favoriteColor: "Vue Green"
})
you should do this:

vm.userProfile = Object.assign ( {} , vm.userProfile , {
age: 27,
favoriteColor: "Vue Green"
})
= split line =

is there a difference between the two ways of writing? It will destroy vm.userProfile anyway

Mar.01,2021

the difference is that if vm.userProfile is undefined, an error will be reported in the first case.
there are two situations that change vm.userProfile, but you can understand that the first is to add {age: 27 favoriteColor: 'Vue Green'} to vm.userProfile after traversing without changing the address pointed to by the original vm.userProfile, and the second is to add vm.userProfile and {age: 27 favoriteColor:' Vue Green'} to this new {} after traversing, and vm.userProfile actually points to a new address.
this is the simplest comparison:

//  a === vm.userProfiletruevm.userProfile
var a = Object.assign(vm.userProfile, {
age: 27,
favoriteColor: 'Vue Green'
})
// b === vm.userProfile false
var b = Object.assign({}, vm.userProfile, {
age: 27,
favoriteColor: 'Vue Green'
})

because I haven't used vue, I don't know what kind of considerations are involved in vue. But if you are in react, the front and back vm.userProfile are different, so you can compare the difference between the two and then re-render the component.


actually, it's not impossible. If you look at the official website, the wording of the document is also suggestion . The specific reason for doing this is because it follows the development concept of one-way data flow, and most of the current mvvm frameworks follow f (data) = > state . If you make changes directly on the same vm, it may cause unpredictable changes to pages that have been rendered with this vm as a data source, and it will be more difficult to debug in more complex applications.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b2c258-2bae1.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b2c258-2bae1.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?