There are two important keywords in the above sentence:
Before we go ahead and understand purpose of Object.assign, it is essential that we really understand these two words, enumerable properties and own properties. Let us see them one by one:
There are two properties in babycat object. By default both are enumerable, hence as the output of for..in loop, you will get both age and name printed.
Now let us change default enumerable behavior of age property using Object.defineProperty method.
We have changed enumerable of age property to false, so as output, you will get only color printed. Hence, age is no longer an enumerable property of babycat object.
In above code snippet, there are two objects cat and babycat. In addition, [[Prototype]] property of babycat object is set to cat object. When you print properties of the babycat object using for..in loop, you will find as output age, color, name printed as shown in below image:
You should able to infer what exactly above sentence implies. Consider code below :
Using Object.assign() method , we are copying cat object own enumerable properties to babycat object. Here, cat object is source and babycat object is target. You will get output printed as below:
The Object.assign() method uses [[Get]] on the source object and [[set]] on the target object and invokes setter and getter to perform the task. Essentially, it assigns properties values from source to target object. It does not create new property in the target object.
As of now you know purpose of the Object.assign() method. Let us examine some variations while copying properties from source object to a target object.
Same Properties in both target and source object
If target object has same properties as of source object, then Object.assign() method will override target object properties. To understand it consider code listing below:
There is age property in both target object and source object. While copying properties in target object the Object.assign() method will override target object age property, hence you will get output as shown in the below image :