What does { …obj1, obj2 } do exactly [duplicate]

三世轮回 提交于 2019-12-08 21:57:27
wentjun

This is actually from ECMAScript 2018's spread syntax and ECMAScript 2015's object destructuring.

{ ...state, animals } creates a shallow copy of the state object, with a new property called animals (with the value of animals object within it).

Since you are a Vuex user, this conforms to the rule of immutable update patterns, which is to prevent the 'original' state object from being altered or mutated. You should read up on the ways of handling common operations such as add/update/delete, using the immutable pattern.

It means Object.assign({}, state, { animals: animals} }.

What is does is it spreads all the properties of state into the new object - making a shallow copy. All the properties in the original object (state) will be copied into the new object (the one you're passing into replaceState). Here's a simple demonstration:

let obj1 = { obj: "1" };
let obj2 = { ...obj1, obj2: true };
console.log(obj1);
console.log(obj2);

The animals bit is ES6 property shorthand, and will essentially do this:

animals: animals

It's just cleaner syntax.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!