Usage of Object.assign() in JS

Object.assign() is a shallow copy

Merge object:

var o1 = {
    
     a: 1 };
var o2 = {
    
     b: 2 };
var o3 = {
    
     c: 3 };

var obj = Object.assign(o1, o2, o3);
console.log(obj); // {
    
     a: 1, b: 2, c: 3 }
console.log(o1); // {
    
     a: 1, b: 2, c: 3 }, 注意目标对象自身也会改变。

Skills in Vue

Because Object.assign() has the above characteristics, we can use it in Vue like this:

The Vue component may have such a requirement: In some cases, the data data of the Vue component needs to be reset. At this point, we can this.$dataget the data in the current state and the data in this.$options.data()the initial state of the component. Then just use Object.assign(this.$data, this.$options.data())it to reset the current state of the data to the initial state, which is very convenient!

Guess you like

Origin blog.csdn.net/sinat_34241861/article/details/113845312