1. What are the benefits of using clone?
Answer: The benefits include the following:
- Easy to use: If you want to copy an object, but some of the properties in this object have been modified, if you don't use cloning, you need to manually assign values to the properties, which is much more troublesome than cloning;
- High performance: Looking at the clone method, you can know that it is a native method, and the native method is a native function, implemented in the language of the bottom of the operating system, so the execution efficiency is higher;
- Isolation: Cloning can ensure that objects are isolated from each other during operation.
clone() source code, as shown below:
2. What is the difference between shallow clone and deep clone?
Answer: The difference is mainly in the duplication of reference types. The specific information is as follows:
- Shallow cloning: Only the value type of the object is copied, but the reference type of the object is not copied;
- Deep clone: Copy the entire object, including value types and reference types.