Home > Web Front-end > JS Tutorial > body text

Javascript shallow copy and deep copy implementation code_javascript skills

WBOY
Release: 2016-05-16 18:57:57
Original
1809 people have browsed it

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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!