JS: Objects
Theory: Spread and creating new objects
Cloning and merging can be combined into one operation that allows us to "update" objects in a functional style, in other words, to create new objects based on the old ones instead of mutating them. We will learn more about such tasks closer to the end of the profession when we talk about frameworks. Below is an example of such an operation:
Modern JavaScript has a special operator that allows you to perform the same task a little more quickly and, most importantly, more clearly. It's called the spread operator. Simple shallow copying using spread:
A spread statement is three dots before a variable (or constant) name inside an object definition It places the corresponding object inside the new object. By using it, you can only get a copy, it cannot change existing objects.
Thanks to the spread operator, it's easy to expand new objects with additional data:
Anything that appears on the right side of the spread will have merge priority, similar to how Object.assign() works. In turn, everything on the left has a lower priority:
Properties can appear both to the left and to the right of this operator at the same time:
The spread operator itself can be used within one object any number of times:
Finally, you can also combine new properties and several spreads together:
Recommended programs
Completed
0 / 13

