Object.assign() usage summary

Object.assign() usage

1. The Object.assign() method is used to assign the values ​​of all enumerable properties from one or more source objects to the target object. It will return the target object.
2. The Object.assign method implements shallow copy, not deep copy. What is copied from the target object is a reference to this object
3. Syntax: Object.assign(target, …sources)

1. Example 1
code

let aaa = {
    
    
 text: 2,
 value: 11,
}

let bbb = {
    
    
  text: 3
}

let ccc = Object.assign(aaa,bbb) // aaa目标对象, bbb源对象

console.log(aaa)
console.log(bbb)
console.log(ccc)

Summary of output results
Insert image description here
: When Object.assign() merges objects, if there are keys with the same name and different values, the merged value (ccc) of the key will be the value of the source object (bbb), and the target object (aaa) The value of

2. Example 2
code

let aaa = {
    
    
      text: 2,
      value: 11
    }
    let bbb = {
    
    
      text: 3
    }
    let ccc = Object.assign(bbb,aaa)
    console.log(aaa)
    console.log(bbb)
    console.log(ccc)

Output results
Insert image description here

Summary: When Object.assign() merges objects, if there are keys with the same name and different values, the merged value (ccc) of the key will be the value of the source object (aaa), and the value of the target object (bbb) also changed

Guess you like

Origin blog.csdn.net/TurtleOrange/article/details/120563264