Home  >  Article  >  Web Front-end  >  Javascript shallow copy and deep copy implementation code_javascript skills

Javascript shallow copy and deep copy implementation code_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:57:571810browse

What is "clone"?
In the actual programming process, we often encounter this situation: there is an object A. At a certain moment, A already contains some valid values. At this time, a new object B that is exactly the same as A may be needed. And any subsequent changes to B will not affect the value in A. That is to say, A and B are two independent objects, but the initial value of B is determined by the A object. In the Java/javascript language, this need cannot be met with a simple assignment statement. Although there are many ways to meet this demand, implementing the clone() method is the simplest and most efficient method. Of course, there is no such method in the JavaScript language.
So I specially wrote two cloning methods: One is a shallow copy and the other is a deep copy.
Explanation:
Shallow copy (shadow clone): Only the basic type and object type of the object are copied, which still belongs to the original reference.
Deep copy (deep clone) : Not only the basic class of the object is copied, but also the objects in the original object are copied. That is to say, the new object is completely generated.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn