Sometimes when operating on arrays and the like, the clone method is used. I feel that it is okay to assign values directly without using this. What are the benefits of doing so? To save memory? There are also shallow copies, deep copies, etc., forgive me for being a novice. .
The advantage is that unlike "=", the clone array is independent in memory, and you can do whatever you want with it.
In js, arrays and objects are reference types.
So, the purpose of cloning is to generate a new copy of data without contaminating the original data.
As for deep copy and shallow copy:
As you can see, the difference between shallow copy and deep copy is that shallow copy only traverses the first layer of obj1, and then assigns each attribute of obj1 to obj2; while deep copy does not, when the attribute value is an object At this time, the deep copy will create a new empty object, then assign the object's value to the empty object, and then return the empty object as the attribute of obj3.
A very important principle in JS programming is"Don't touch objects that are not yours"
To give you a vivid metaphor, I lent you a book (object), and I thought you just wanted to read it (read), but you used it to practice calligraphy (write). I can’t even read the book when you return it to me. Normal content.
And deepClone solves this problem. I give you a book, you go and copy it, return the book to me as is, and you can dispose of the copy as you wish.
Deep copy and shallow copy in javascript?