Continue with the last post, issue about deep clone in Javascript object.
Here is a test code snippet for shallow clone or deep clone.
1 | /** |
For testing array code part, I am really amazed that the cloned array is independent from the original array.
Maybe there is no deep or shallow clone difference in Javascript array objects.
However, for testing object code part, the difference does exist.
Trap 1
In this place, a type check should be added. This version is simplified.
Among objects, there are mainly three sub types that need attention.
1 | typeof [1,2,3]; // object |
Therefore, for trap 1 case, an instanceof check should be added, to decide whether the result structure should be array or object.